pCOS Cookbook

cookbook

interactive/geospatial

Retrieve geospatial reference information from a GeoPDF document.

Download Java Code     Show Output     Show Input PDF

package com.pdflib.cookbook.pcos.interactive;

import com.pdflib.IpCOS;
import com.pdflib.cookbook.pcos.pcos_cookbook_example;

/**
 * Retrieve geospatial reference information from a GeoPDF document
 * <p>
 * This pCOS sample searches for several different kinds of geospatial
 * information: three Flavors according to Adobe PDF 1.7 extension level 3
 * (these are equivalent to ISO 32000-2), and the proprietary TerraGo format:
 * <p>
 * 
 * (1) Page-based ISO viewports
 * <p>
 * This flavor can be created with Acrobat 9 by manually adding geospatial
 * reference information to a page. In PDFlib 8 it can be created with the
 * "viewports" option of PDF_begin/end_page_ext().
 * 
 * (2) Image-based ISO viewports
 * <p>
 * This flavor can be created with Acrobat 9 by converting raster images (e.g.
 * GeoTIFF) to PDF. In PDFlib 8 it can be created with the "georeference" option
 * of PDF_load_image().
 * 
 * (3) XObject-based ISO viewports
 * <p>
 * This flavor is specified in ISO as well, but unfortunately it doesn't work in
 * Acrobat 9. It could be created with PDFlib 8, but this feature is disabled
 * since the resulting GeoPDF documents wouldn't work in Acrobat 9.
 * 
 * (4) Page-based geospatial information created by TerraGo.
 * <p>
 * This flavor is created by the TerraGo software and is accepted by Acrobat 9
 * in addition to the ISO flavors.
 * <p>
 * <p>
 * Required software: pCOS interface 3 (pCOS 3.x, PDFlib+PDI/PPS 7.x, TET 2.2,
 * PLOP 3.x) <br>
 * Required data: PDF document
 * 
 * @version $Id: geospatial.java,v 1.4 2015/11/16 11:53:16 stm Exp $
 */

public class geospatial extends pcos_cookbook_example {

    /* This is where the data files are. Adjust as necessary. */
    private final static String SEARCH_PATH = "../input";

    public void example_code(IpCOS p, int doc) throws 
        Exception {

        String path;
        String basepath;
        String objtype;

        System.out.println("File name: " + p.pcos_get_string(doc, "filename"));

        int pagecount = (int) p.pcos_get_number(doc, "length:pages");

        for (int page = 0; page < pagecount; page++) {
            System.out.println("page " + (page + 1) + ": ");

            /*
             * ************************************************************
             * (1) Page-based ISO viewports
             * ************************************************************
             */

            /* Iterate over all viewports which may be present on the page */
            basepath = "pages[" + page + "]/VP";
            int vpcount = (int) p.pcos_get_number(doc, "length:" + basepath);

            for (int v = 0; v < vpcount; v++) {
                System.out.println("  ISO viewport " + v + " on page:");
                dump_iso_viewport(p, basepath + "[" + v + "]", doc);
            }

            /*
             * ************************************************************
             * (2) Image-based ISO viewports
             * ************************************************************
             */

            /* Iterate over all images on the page */
            basepath = "pages[" + page + "]/images";
            int imagecount = (int) p.pcos_get_number(doc, "length:" + basepath);

            for (int image = 0; image < imagecount; image++) {
                path = basepath + "[" + image + "]/Measure/Subtype";
                objtype = p.pcos_get_string(doc, "type:" + path);

                if (objtype.equals("name")) {
                    String subtype = p.pcos_get_string(doc, path);

                    if (subtype.equals("GEO")) {
                        System.out.println("  ISO viewport on image " + image
                            + ": ");
                        basepath = "pages[" + page + "]/images[" + image + "]";
                        dump_iso_viewport(p, basepath, doc);
                    }
                }
            }

            /*
             * ************************************************************
             * (3) XObject-based ISO viewports
             * ************************************************************
             */
            /* Iterate over all Form XObjects on the page */
            basepath = "pages[" + page + "]/Resources/XObject";
            int xobjcount = (int) p.pcos_get_number(doc, "length:" + basepath);

            for (int xobj = 0; xobj < xobjcount; xobj++) {
                String subtype = p.pcos_get_string(doc, basepath + "[" + xobj
                    + "]/Subtype");

                /* Consider Form XObjects only (as opposed to Image XObjects */
                if (subtype.equals("Form")) {
                    path = basepath + "[" + xobj + "]/Measure/Subtype";
                    objtype = p.pcos_get_string(doc, "type:" + path);

                    if (objtype.equals("name")) {
                        subtype = p.pcos_get_string(doc, path);

                        if (subtype.equals("GEO")) {
                            System.out.println("  viewport on Form XObject "
                                + xobj + ": ");
                            basepath = "pages[" + page + "]/images[" + xobj
                                + "]";
                            dump_iso_viewport(p, basepath, doc);
                        }
                    }
                }
            }

            /*
             * ************************************************************
             * (4) Page-based TerraGo viewports
             * ************************************************************
             */

            /*
             * Iterate over all TerraGo viewports which may be present on the
             * page
             */
            path = "pages[" + page + "]/LGIDict";
            int tgcount = (int) p.pcos_get_number(doc, "length:" + path);

            for (int tg = 0; tg < tgcount; tg++) {
                System.out.println("  TerraGo viewport " + tg + ": ");
                basepath = path + "[" + tg + "]";
                dump_terrago_viewport(p, basepath, doc);
            }
        }
    }

    /* Emit some ISO viewport properties; can easily be extended */
    private void dump_iso_viewport(IpCOS p, String basepath, int doc)
        throws Exception {
        String path;
        String objtype;

        path = basepath + "/Measure/GCS/WKT";
        objtype = p.pcos_get_string(doc, "type:" + path);
        if (objtype.equals("string")) {
            System.out
                .println("    WKT='" + p.pcos_get_string(doc, path) + "'");
        }

        path = basepath + "/Measure/GCS/EPSG";
        objtype = p.pcos_get_string(doc, "type:" + path);
        if (objtype.equals("number")) {
            System.out
                .println("    EPSG=" + (int) p.pcos_get_number(doc, path));
        }

        /* Print the world coordinates for two points */
        path = basepath + "/Measure/GCS/GPTS[0]";
        System.out.println("    lat,lon="
            + p.pcos_get_number(doc, basepath + "/Measure/GPTS[0]") + ","
            + p.pcos_get_number(doc, basepath + "/Measure/GPTS[1]") + " to "
            + p.pcos_get_number(doc, basepath + "/Measure/GPTS[4]") + ","
            + p.pcos_get_number(doc, basepath + "/Measure/GPTS[5]"));

    }

    /* Emit some Terrago viewport properties; can easily be extended */
    private void dump_terrago_viewport(IpCOS p, String basepath, int doc)
        throws Exception {
        String path;
        String objtype;

        path = basepath + "/Description";
        objtype = p.pcos_get_string(doc, "type:" + path);

        if (objtype.equals("string")) {
            System.out.println("    description='"
                + p.pcos_get_string(doc, path) + "'");
        }

        path = basepath + "/Display/Datum";
        objtype = p.pcos_get_string(doc, "type:" + path);

        if (objtype.equals("string")) {
            System.out.println("    display datum='"
                + p.pcos_get_string(doc, path) + "'");
        }

        path = basepath + "/Projection/ProjectionType";
        objtype = p.pcos_get_string(doc, "type:" + path);

        if (objtype.equals("string")) {
            System.out.println("    projection type='"
                + p.pcos_get_string(doc, path) + "'");
        }
    }

    public geospatial(String[] argv, String readable_name, String search_path,
        String full_rcs_file_name, String revision) {
        super(argv, readable_name, search_path, full_rcs_file_name, revision);
    }

    public static void main(String argv[]) {
        geospatial example = new geospatial(argv, "Geospatial data",
            SEARCH_PATH, "$RCSfile: geospatial.java,v $", "$Revision: 1.4 $");
        example.execute();
    }
}