Package org.gdal.ogr

Examples of org.gdal.ogr.Geometry


            /* -------------------------------------------------------------------- */
            /*      Get layer extents, and create a corresponding polygon           */
            /*      geometry.                                                       */
            /* -------------------------------------------------------------------- */
            double sExtents[] = poLayer.GetExtent(true);
            Geometry/*Polygon*/ oRegion = new Geometry(ogr.wkbPolygon);
            Geometry/*LinearRing*/ oRing = new Geometry(ogr.wkbLinearRing);
            
            if (sExtents == null) {
               System.err.print("GetExtent() failed on layer "+poLayer.GetLayerDefn().GetName()+" of "+args[nFirstSourceDataset]+", skipping.\n");
               continue;
            }
                                   
            // XXX: sExtents [minX, maxX, minY, maxY]
            //oRing.addPoint( sExtents.MinX, sExtents.MinY );
            //oRing.addPoint( sExtents.MinX, sExtents.MaxY );
            //oRing.addPoint( sExtents.MaxX, sExtents.MaxY );
            //oRing.addPoint( sExtents.MaxX, sExtents.MinY );
            //oRing.addPoint( sExtents.MinX, sExtents.MinY );
            oRing.AddPoint_2D( sExtents[0], sExtents[2] );
            oRing.AddPoint_2D( sExtents[0], sExtents[3] );
            oRing.AddPoint_2D( sExtents[1], sExtents[3] );
            oRing.AddPoint_2D( sExtents[1], sExtents[2] );
            oRing.AddPoint_2D( sExtents[0], sExtents[2] );

            oRegion.AddGeometry( oRing );

            /* -------------------------------------------------------------------- */
            /*      Add layer to tileindex.                                         */
 
View Full Code Here


        srcLayer.ResetReading();

        while ((feature = srcLayer.GetNextFeature()) != null) {

            Geometry geometry = feature.GetGeometryRef();

            if (geometry != null) {

                int geomtype = geometry.GetGeometryType()
                        & (~ogrConstants.wkb25DBit);

                double burnValue = 0.0;

                if (burnFieldIndex >= 0) {

                    burnValue = feature.GetFieldAsDouble(burnFieldIndex);
                }

                if (geomtype == ogr.wkbMultiPoint) {

                    int geomIndex = 0;
                    int geomCount = geometry.GetGeometryCount();

                    for (geomIndex = 0; geomIndex < geomCount; geomIndex++) {

                        ProcessGeometry(geometry.GetGeometryRef(geomIndex),
                                clipSrc, burnFieldIndex, burnValue, X, Y, Z);
                    }

                } else {
View Full Code Here

            String srcWhere) {

        DataSource DS;
        Layer lyr;
        Feature feat;
        Geometry geom = null;

        DS = ogr.Open(srcDS, false);

        if (DS == null) {

            return null;
        }

        if (srcSQL != null) {

            lyr = DS.ExecuteSQL(srcSQL, null, null);
        } else if (srcLyr != null) {

            lyr = DS.GetLayerByName(srcLyr);
        } else {

            lyr = DS.GetLayer(0);
        }

        if (lyr == null) {

            System.err
                    .println("Failed to identify source layer from datasource.");
            DS.delete();
            return null;
        }

        if (srcWhere != null) {

            lyr.SetAttributeFilter(srcWhere);
        }

        while ((feat = lyr.GetNextFeature()) != null) {

            Geometry srcGeom = feat.GetGeometryRef();

            if (srcGeom != null) {

                int srcType = srcGeom.GetGeometryType()
                        & (~ogrConstants.wkb25DBit);

                if (geom == null) {

                    geom = new Geometry(ogr.wkbMultiPolygon);
                }

                if (srcType == ogr.wkbPolygon) {

                    geom.AddGeometry(srcGeom);
                } else if (srcType == ogr.wkbMultiPolygon) {

                    int geomIndex = 0;
                    int geomCount = srcGeom.GetGeometryCount();

                    for (geomIndex = 0; geomIndex < geomCount; geomIndex++) {

                        geom.AddGeometry(srcGeom.GetGeometryRef(geomIndex));
                    }

                } else {

                    System.err
View Full Code Here

        String clipSQL = null;
        String clipLayer = null;
        String clipWhere = null;
        String outputSRS = null;
        String algorithmAndOptions = null;
        Geometry clipSrc = null;
        Geometry spatialFilter = null;
        ProgressCallback progressCallback = null;
        String clipSrcDS = null;
        boolean[] isXExtentSet = new boolean[1];
        boolean[] isYExtentSet = new boolean[1];

        minX[0] = 0.0;
        maxX[0] = 0.0;
        minY[0] = 0.0;
        maxY[0] = 0.0;

        isXExtentSet[0] = false;
        isYExtentSet[0] = false;

        /*
         * Register GDAL and OGR format(s)
         */

        gdal.AllRegister();
        ogr.RegisterAll();

        /*
         * Parse arguments
         */

        args = ogr.GeneralCmdLineProcessor(args);

        if (args.length < 2) {
            Usage();
        }

        for (int i = 0; i < args.length; i++) {
            if (args[i].equals("---utility_version")) {

                System.out.println("Running against GDAL " + gdal.VersionInfo());
                return;

            } else if (args[i].equals("-of") && args.length > i) {

                outputFormat = args[++i];

            } else if (args[i].equals("-q") || args[i].equals("-quiet")) {

                quiet = true;

            } else if (args[i].equals("-ot") && args.length > i) {

                outputType = gdal.GetDataTypeByName(args[++i]);

                if (outputType == gdalconstConstants.GDT_Unknown) {

                    System.err.println("FAILURE: Unknown output pixel type: "
                            + args[i]);
                    Usage();
                }

            } else if (args[i].equals("-txe") && args.length > i + 1) {

                minX[0] = Double.parseDouble(args[++i]);
                maxX[0] = Double.parseDouble(args[++i]);
                isXExtentSet[0] = true;

            } else if (args[i].equals("-tye") && args.length > i + 1) {

                minY[0] = Double.parseDouble(args[++i]);
                maxY[0] = Double.parseDouble(args[++i]);
                isYExtentSet[0] = true;

            } else if (args[i].equals("-outsize") && args.length > i + 1) {

                sizeX = Integer.parseInt(args[++i]);
                sizeY = Integer.parseInt(args[++i]);

            } else if (args[i].equals("-co") && args.length > i) {

                if (createOptions == null) {
                    createOptions = args[++i];
                } else {
                    createOptions += ':' + args[++i];
                }

            } else if (args[i].equals("-zfield") && args.length > i) {

                burnAttribute = args[++i];

            } else if (args[i].equals("-where") && args.length > i) {

                where = args[++i];

            } else if (args[i].equals("-l") && args.length > i) {

                if (layers == null) {
                    layers = args[++i];
                } else {
                    layers += ':' + args[++i];
                }

            } else if (args[i].equals("-sql") && args.length > i) {

                SQL = args[++i];

            } else if (args[i].equals("-spat") && args.length > i + 3) {

                double clipMinX = Double.parseDouble(args[i + 1]);
                double clipMinY = Double.parseDouble(args[i + 2]);
                double clipMaxX = Double.parseDouble(args[i + 3]);
                double clipMaxY = Double.parseDouble(args[i + 4]);
                i += 4;
                clipSrc = new Geometry(ogr.wkbPolygon);
                clipSrc.AddPoint(clipMinX, clipMinY);
                clipSrc.AddPoint(clipMinX, clipMaxY);
                clipSrc.AddPoint(clipMaxX, clipMaxY);
                clipSrc.AddPoint(clipMaxX, clipMinY);
                clipSrc.AddPoint(clipMinX, clipMinY);

            } else if (args[i].equals("-clipsrc") && args.length > i) {

                hasClipSrc = true;

                try {

                    double clipMinX = Double.parseDouble(args[i + 1]);
                    double clipMinY = Double.parseDouble(args[i + 2]);
                    double clipMaxX = Double.parseDouble(args[i + 3]);
                    double clipMaxY = Double.parseDouble(args[i + 4]);
                    i += 4;
                    clipSrc = new Geometry(ogr.wkbPolygon);
                    clipSrc.AddPoint(clipMinX, clipMinY);
                    clipSrc.AddPoint(clipMinX, clipMaxY);
                    clipSrc.AddPoint(clipMaxX, clipMaxY);
                    clipSrc.AddPoint(clipMaxX, clipMinY);
                    clipSrc.AddPoint(clipMinX, clipMinY);

                } catch (NumberFormatException e) {

                    if (args[i].substring(0, 6).equals("POLYGON")
                            || args[i].substring(0, 11).equals("MULTIPOLYGON")) {

                        clipSrc = ogr.CreateGeometryFromWkt(args[++i]);

                    } else if (args[i].equals("spat_extent")) {

                        ++i;

                    } else {

                        clipSrcDS = args[++i];
                    }
                }

            } else if (args[i].equals("-clipsrcsql") && args.length > i) {

                clipSQL = args[++i];

            } else if (args[i].equals("-clipsrclayer") && args.length > i) {

                clipLayer = args[++i];

            } else if (args[i].equals("-clipsrcwhere") && args.length > i) {

                clipLayer = args[++i];

            } else if (args[i].equals("-a_srs") && args.length > i) {

                SpatialReference enteredSRS = new SpatialReference();

                if (enteredSRS.SetFromUserInput(args[i + 1]) != 0) {
                    System.err.println("Failed to process SRS definition: "
                            + args[i + 1]);
                    Usage();
                }
                i++;

                outputSRS = enteredSRS.ExportToWkt();

            } else if (args[i].equals("-a") && args.length > i) {

                if (algorithmAndOptions == null) {
                    algorithmAndOptions = args[++i];
                } else {
                    algorithmAndOptions += ':' + args[++i];
                }

            } else if (args[i].substring(0, 1).equals("-")) {

                System.err.println("FAILURE: Option " + args[i]
                        + "incomplete, or not recognised");
                Usage();

            } else if (sourceFilename == null) {

                sourceFilename = args[i];

            } else if (outputFilename == null) {

                outputFilename = args[i];

            } else {

                Usage();

            }
        }

        if (sourceFilename == null || outputFilename == null
                || (SQL == null && layers == null)) {

            Usage();
        }

        /*
         * Open Input
         */

        if (hasClipSrc && clipSrcDS != null) {

            clipSrc = LoadGeometry(clipSrcDS, clipSQL, clipLayer, clipWhere);

            if (clipSrc == null) {

                System.out.println("FAILURE: cannot load source clip geometry");
                Usage();
            }

        } else if (hasClipSrc && clipSrcDS == null) {

            clipSrc = spatialFilter.Clone();

            if (clipSrc == null) {

                System.out
                        .println("FAILURE: "
View Full Code Here

        String pszSourceSRSDef = null;
        SpatialReference poOutputSRS = null;
        SpatialReference poSourceSRS = null;
        String pszNewLayerName = null;
        String pszWHERE = null;
        Geometry poSpatialFilter = null;
        String pszSelect;
        Vector papszSelFields = null;
        String pszSQLStatement = null;
        int    eGType = -2;
        GeomOperation eGeomOp = GeomOperation.NONE;
        double dfGeomOpParam = 0;
        Vector papszFieldTypesToString = new Vector();
        boolean bDisplayProgress = false;
        ProgressCallback pfnProgress = null;
        boolean  bClipSrc = false;
        Geometry poClipSrc = null;
        String pszClipSrcDS = null;
        String pszClipSrcSQL = null;
        String pszClipSrcLayer = null;
        String pszClipSrcWhere = null;
        Geometry poClipDst = null;
        String pszClipDstDS = null;
        String pszClipDstSQL = null;
        String pszClipDstLayer = null;
        String pszClipDstWhere = null;
        String pszSrcEncoding = null;
        String pszDstEncoding = null;
        boolean bExplodeCollections = false;
        String pszZField = null;

        ogr.DontUseExceptions();

    /* -------------------------------------------------------------------- */
    /*      Register format(s).                                             */
    /* -------------------------------------------------------------------- */
        if( ogr.GetDriverCount() == 0 )
            ogr.RegisterAll();
   
    /* -------------------------------------------------------------------- */
    /*      Processing command line arguments.                              */
    /* -------------------------------------------------------------------- */
        args = ogr.GeneralCmdLineProcessor( args );
       
        if( args.length < 2 )
        {
            Usage();
            System.exit( -1 );
        }
   
        for( int iArg = 0; iArg < args.length; iArg++ )
        {
            if( args[iArg].equalsIgnoreCase("-f") && iArg < args.length-1 )
            {
                pszFormat = args[++iArg];
            }
            else if( args[iArg].equalsIgnoreCase("-dsco") && iArg < args.length-1 )
            {
                papszDSCO.addElement(args[++iArg] );
            }
            else if( args[iArg].equalsIgnoreCase("-lco") && iArg < args.length-1 )
            {
                papszLCO.addElement(args[++iArg] );
            }
            else if( args[iArg].equalsIgnoreCase("-preserve_fid") )
            {
                bPreserveFID = true;
            }
            else if( args[iArg].length() >= 5 && args[iArg].substring(0, 5).equalsIgnoreCase("-skip") )
            {
                bSkipFailures = true;
                nGroupTransactions = 1; /* #2409 */
            }
            else if( args[iArg].equalsIgnoreCase("-append") )
            {
                bAppend = true;
                bUpdate = true;
            }
            else if( args[iArg].equalsIgnoreCase("-overwrite") )
            {
                bOverwrite = true;
                bUpdate = true;
            }
            else if( args[iArg].equalsIgnoreCase("-update") )
            {
                bUpdate = true;
            }
            else if( args[iArg].equalsIgnoreCase("-fid") && args[iArg+1] != null )
            {
                nFIDToFetch = Integer.parseInt(args[++iArg]);
            }
            else if( args[iArg].equalsIgnoreCase("-sql") && args[iArg+1] != null )
            {
                pszSQLStatement = args[++iArg];
            }
            else if( args[iArg].equalsIgnoreCase("-nln") && iArg < args.length-1 )
            {
                pszNewLayerName = args[++iArg];
            }
            else if( args[iArg].equalsIgnoreCase("-nlt") && iArg < args.length-1 )
            {
                if( args[iArg+1].equalsIgnoreCase("NONE") )
                    eGType = ogr.wkbNone;
                else if( args[iArg+1].equalsIgnoreCase("GEOMETRY") )
                    eGType = ogr.wkbUnknown;
                else if( args[iArg+1].equalsIgnoreCase("POINT") )
                    eGType = ogr.wkbPoint;
                else if( args[iArg+1].equalsIgnoreCase("LINESTRING") )
                    eGType = ogr.wkbLineString;
                else if( args[iArg+1].equalsIgnoreCase("POLYGON") )
                    eGType = ogr.wkbPolygon;
                else if( args[iArg+1].equalsIgnoreCase("GEOMETRYCOLLECTION") )
                    eGType = ogr.wkbGeometryCollection;
                else if( args[iArg+1].equalsIgnoreCase("MULTIPOINT") )
                    eGType = ogr.wkbMultiPoint;
                else if( args[iArg+1].equalsIgnoreCase("MULTILINESTRING") )
                    eGType = ogr.wkbMultiLineString;
                else if( args[iArg+1].equalsIgnoreCase("MULTIPOLYGON") )
                    eGType = ogr.wkbMultiPolygon;
                else if( args[iArg+1].equalsIgnoreCase("GEOMETRY25D") )
                    eGType = ogr.wkbUnknown | ogr.wkb25DBit;
                else if( args[iArg+1].equalsIgnoreCase("POINT25D") )
                    eGType = ogr.wkbPoint25D;
                else if( args[iArg+1].equalsIgnoreCase("LINESTRING25D") )
                    eGType = ogr.wkbLineString25D;
                else if( args[iArg+1].equalsIgnoreCase("POLYGON25D") )
                    eGType = ogr.wkbPolygon25D;
                else if( args[iArg+1].equalsIgnoreCase("GEOMETRYCOLLECTION25D") )
                    eGType = ogr.wkbGeometryCollection25D;
                else if( args[iArg+1].equalsIgnoreCase("MULTIPOINT25D") )
                    eGType = ogr.wkbMultiPoint25D;
                else if( args[iArg+1].equalsIgnoreCase("MULTILINESTRING25D") )
                    eGType = ogr.wkbMultiLineString25D;
                else if( args[iArg+1].equalsIgnoreCase("MULTIPOLYGON25D") )
                    eGType = ogr.wkbMultiPolygon25D;
                else
                {
                    System.err.println("-nlt " + args[iArg+1] + ": type not recognised.");
                    System.exit( 1 );
                }
                iArg++;
            }
            else if( (args[iArg].equalsIgnoreCase("-tg") ||
                    args[iArg].equalsIgnoreCase("-gt")) && iArg < args.length-1 )
            {
                nGroupTransactions = Integer.parseInt(args[++iArg]);
            }
            else if( args[iArg].equalsIgnoreCase("-s_srs") && iArg < args.length-1 )
            {
                pszSourceSRSDef = args[++iArg];
            }
            else if( args[iArg].equalsIgnoreCase("-a_srs") && iArg < args.length-1 )
            {
                pszOutputSRSDef = args[++iArg];
            }
            else if( args[iArg].equalsIgnoreCase("-t_srs") && iArg < args.length-1 )
            {
                pszOutputSRSDef = args[++iArg];
                bTransform = true;
            }
            else if (args[iArg].equalsIgnoreCase("-spat") && iArg + 4 < args.length)
            {
                Geometry oRing = new Geometry(ogrConstants.wkbLinearRing);
                double xmin = new Double(args[++iArg]).doubleValue();
                double ymin = new Double(args[++iArg]).doubleValue();
                double xmax = new Double(args[++iArg]).doubleValue();
                double ymax = new Double(args[++iArg]).doubleValue();
                oRing.AddPoint(xmin, ymin);
                oRing.AddPoint(xmin, ymax);
                oRing.AddPoint(xmax, ymax);
                oRing.AddPoint(xmax, ymin);
                oRing.AddPoint(xmin, ymin);
               
                poSpatialFilter = new Geometry(ogrConstants.wkbPolygon);
                poSpatialFilter.AddGeometry(oRing);
            }
            else if( args[iArg].equalsIgnoreCase("-where") && args[iArg+1] != null )
            {
                pszWHERE = args[++iArg];
            }
            else if( args[iArg].equalsIgnoreCase("-select") && args[iArg+1] != null)
            {
                pszSelect = args[++iArg];
                StringTokenizer tokenizer = new StringTokenizer(pszSelect, " ,");
                papszSelFields = new Vector();
                while(tokenizer.hasMoreElements())
                    papszSelFields.addElement(tokenizer.nextToken());
            }
            else if( args[iArg].equalsIgnoreCase("-simplify") && iArg < args.length-1 )
            {
                eGeomOp = GeomOperation.SIMPLIFY_PRESERVE_TOPOLOGY;
                dfGeomOpParam = new Double(args[++iArg]).doubleValue();
            }
            else if( args[iArg].equalsIgnoreCase("-segmentize") && iArg < args.length-1 )
            {
                eGeomOp = GeomOperation.SEGMENTIZE;
                dfGeomOpParam = new Double(args[++iArg]).doubleValue();
            }
            else if( args[iArg].equalsIgnoreCase("-fieldTypeToString") && iArg < args.length-1 )
            {
                StringTokenizer tokenizer = new StringTokenizer(args[++iArg], " ,");
                while(tokenizer.hasMoreElements())
                {
                    String token = (String)tokenizer.nextToken();
                    if (token.equalsIgnoreCase("Integer") ||
                        token.equalsIgnoreCase("Real") ||
                        token.equalsIgnoreCase("String") ||
                        token.equalsIgnoreCase("Date") ||
                        token.equalsIgnoreCase("Time") ||
                        token.equalsIgnoreCase("DateTime") ||
                        token.equalsIgnoreCase("Binary") ||
                        token.equalsIgnoreCase("IntegerList") ||
                        token.equalsIgnoreCase("RealList") ||
                        token.equalsIgnoreCase("StringList"))
                    {
                        papszFieldTypesToString.addElement(token);
                    }
                    else if (token.equalsIgnoreCase("All"))
                    {
                        papszFieldTypesToString = null;
                        papszFieldTypesToString.addElement("All");
                        break;
                    }
                    else
                    {
                        System.err.println("Unhandled type for fieldtypeasstring option : " + token);
                        Usage();
                    }
                }
            }
            else if( args[iArg].equalsIgnoreCase("-progress") )
            {
                bDisplayProgress = true;
            }
            /*else if( args[iArg].equalsIgnoreCase("-wrapdateline") )
            {
                bWrapDateline = true;
            }
            */
            else if( args[iArg].equalsIgnoreCase("-clipsrc") && iArg < args.length-1 )
            {
                bClipSrc = true;
                if ( IsNumber(args[iArg+1]) && iArg < args.length - 4 )
                {
                    Geometry oRing = new Geometry(ogrConstants.wkbLinearRing);
                    double xmin = new Double(args[++iArg]).doubleValue();
                    double ymin = new Double(args[++iArg]).doubleValue();
                    double xmax = new Double(args[++iArg]).doubleValue();
                    double ymax = new Double(args[++iArg]).doubleValue();
                    oRing.AddPoint(xmin, ymin);
                    oRing.AddPoint(xmin, ymax);
                    oRing.AddPoint(xmax, ymax);
                    oRing.AddPoint(xmax, ymin);
                    oRing.AddPoint(xmin, ymin);
                   
                    poClipSrc = new Geometry(ogrConstants.wkbPolygon);
                    poClipSrc.AddGeometry(oRing);
                }
                else if( (args[iArg+1].length() >= 7 && args[iArg+1].substring(0, 7).equalsIgnoreCase("POLYGON") ) ||
                         (args[iArg+1].length() >= 12 && args[iArg+1].substring(0, 12).equalsIgnoreCase("MULTIPOLYGON") ) )
                {
                    poClipSrc = Geometry.CreateFromWkt(args[iArg+1]);
                    if (poClipSrc == null)
                    {
                        System.err.print("FAILURE: Invalid geometry. Must be a valid POLYGON or MULTIPOLYGON WKT\n\n");
                        Usage();
                    }
                    iArg ++;
                }
                else if (args[iArg+1].equalsIgnoreCase("spat_extent") )
                {
                    iArg ++;
                }
                else
                {
                    pszClipSrcDS = args[iArg+1];
                    iArg ++;
                }
            }
            else if( args[iArg].equalsIgnoreCase("-clipsrcsql") && iArg < args.length-1 )
            {
                pszClipSrcSQL = args[iArg+1];
                iArg ++;
            }
            else if( args[iArg].equalsIgnoreCase("-clipsrclayer") && iArg < args.length-1 )
            {
                pszClipSrcLayer = args[iArg+1];
                iArg ++;
            }
            else if( args[iArg].equalsIgnoreCase("-clipsrcwhere") && iArg < args.length-1 )
            {
                pszClipSrcWhere = args[iArg+1];
                iArg ++;
            }
            else if( args[iArg].equalsIgnoreCase("-clipdst") && iArg < args.length-1 )
            {
                if ( IsNumber(args[iArg+1]) && iArg < args.length - 4 )
                {
                    Geometry oRing = new Geometry(ogrConstants.wkbLinearRing);
                    double xmin = new Double(args[++iArg]).doubleValue();
                    double ymin = new Double(args[++iArg]).doubleValue();
                    double xmax = new Double(args[++iArg]).doubleValue();
                    double ymax = new Double(args[++iArg]).doubleValue();
                    oRing.AddPoint(xmin, ymin);
                    oRing.AddPoint(xmin, ymax);
                    oRing.AddPoint(xmax, ymax);
                    oRing.AddPoint(xmax, ymin);
                    oRing.AddPoint(xmin, ymin);
                   
                    poClipDst = new Geometry(ogrConstants.wkbPolygon);
                    poClipDst.AddGeometry(oRing);
                }
                else if( (args[iArg+1].length() >= 7 && args[iArg+1].substring(0, 7).equalsIgnoreCase("POLYGON") ) ||
                         (args[iArg+1].length() >= 12 && args[iArg+1].substring(0, 12).equalsIgnoreCase("MULTIPOLYGON") ) )
                {
View Full Code Here

                                  String pszWhere)
    {
        DataSource       poDS;
        Layer            poLyr;
        Feature          poFeat;
        Geometry         poGeom = null;
           
        poDS = ogr.Open( pszDS, false );
        if (poDS == null)
            return null;
   
        if (pszSQL != null)
            poLyr = poDS.ExecuteSQL( pszSQL, null, null );
        else if (pszLyr != null)
            poLyr = poDS.GetLayerByName(pszLyr);
        else
            poLyr = poDS.GetLayer(0);
           
        if (poLyr == null)
        {
            System.err.print("Failed to identify source layer from datasource.\n");
            poDS.delete();
            return null;
        }
       
        if (pszWhere != null)
            poLyr.SetAttributeFilter(pszWhere);
           
        while ((poFeat = poLyr.GetNextFeature()) != null)
        {
            Geometry poSrcGeom = poFeat.GetGeometryRef();
            if (poSrcGeom != null)
            {
                int eType = wkbFlatten(poSrcGeom.GetGeometryType());
               
                if (poGeom == null)
                    poGeom = new Geometry( ogr.wkbMultiPolygon );
   
                if( eType == ogr.wkbPolygon )
                    poGeom.AddGeometry( poSrcGeom );
                else if( eType == ogr.wkbMultiPolygon )
                {
                    int iGeom;
                    int nGeomCount = poSrcGeom.GetGeometryCount();
   
                    for( iGeom = 0; iGeom < nGeomCount; iGeom++ )
                    {
                        poGeom.AddGeometry(poSrcGeom.GetGeometryRef(iGeom) );
                    }
                }
                else
                {
                    System.err.print("ERROR: Geometry not of polygon type.\n" );
View Full Code Here

            int nParts = 0;
            int nIters = 1;
            if (bExplodeCollections)
            {
                Geometry poSrcGeometry = poFeature.GetGeometryRef();
                if (poSrcGeometry != null)
                {
                    switch (wkbFlatten(poSrcGeometry.GetGeometryType()))
                    {
                        case ogr.wkbMultiPoint:
                        case ogr.wkbMultiLineString:
                        case ogr.wkbMultiPolygon:
                        case ogr.wkbGeometryCollection:
                            nParts = poSrcGeometry.GetGeometryCount();
                            nIters = nParts;
                            if (nIters == 0)
                                nIters = 1;
                        default:
                            break;
                    }
                }
            }

            for(int iPart = 0; iPart < nIters; iPart++)
            {
   
                if( ++nFeaturesInTransaction == nGroupTransactions )
                {
                    poDstLayer.CommitTransaction();
                    poDstLayer.StartTransaction();
                    nFeaturesInTransaction = 0;
                }

                gdal.ErrorReset();
                poDstFeature = new Feature( poDstLayer.GetLayerDefn() );

                if( poDstFeature.SetFromWithMap( poFeature, 1, panMap ) != 0 )
                {
                    if( nGroupTransactions > 0)
                        poDstLayer.CommitTransaction();

                    System.err.println(
                            "Unable to translate feature " + poFeature.GetFID() + " from layer " +
                            poSrcFDefn.GetName() );

                    poFeature.delete();
                    poFeature = null;
                    poDstFeature.delete();
                    poDstFeature = null;
                    return false;
                }

                if( bPreserveFID )
                    poDstFeature.SetFID( poFeature.GetFID() );

                Geometry poDstGeometry = poDstFeature.GetGeometryRef();
                if (poDstGeometry != null)
                {
                    if (nParts > 0)
                    {
                        /* For -explodecollections, extract the iPart(th) of the geometry */
                        Geometry poPart = poDstGeometry.GetGeometryRef(iPart).Clone();
                        poDstFeature.SetGeometryDirectly(poPart);
                        poDstGeometry = poPart;
                    }

                    if (iSrcZField != -1)
                    {
                        SetZ(poDstGeometry, poFeature.GetFieldAsDouble(iSrcZField));
                        /* This will correct the coordinate dimension to 3 */
                        Geometry poDupGeometry = poDstGeometry.Clone();
                        poDstFeature.SetGeometryDirectly(poDupGeometry);
                        poDstGeometry = poDupGeometry;
                    }

                    if (eGeomOp == GeomOperation.SEGMENTIZE)
                    {
                /*if (poDstFeature.GetGeometryRef() != null && dfGeomOpParam > 0)
                    poDstFeature.GetGeometryRef().segmentize(dfGeomOpParam);*/
                    }
                    else if (eGeomOp == GeomOperation.SIMPLIFY_PRESERVE_TOPOLOGY && dfGeomOpParam > 0)
                    {
                        Geometry poNewGeom = poDstGeometry.SimplifyPreserveTopology(dfGeomOpParam);
                        if (poNewGeom != null)
                        {
                            poDstFeature.SetGeometryDirectly(poNewGeom);
                            poDstGeometry = poNewGeom;
                        }
                    }

                    if (poClipSrc != null)
                    {
                        Geometry poClipped = poDstGeometry.Intersection(poClipSrc);
                        if (poClipped == null || poClipped.IsEmpty())
                        {
                            /* Report progress */
                            nCount ++;
                            if (pfnProgress != null)
                                pfnProgress.run(nCount * 1.0 / nCountLayerFeatures, "");
                            poDstFeature.delete();
                            continue;
                        }
                        poDstFeature.SetGeometryDirectly(poClipped);
                        poDstGeometry = poClipped;
                    }

                    if( poCT != null )
                    {
                        eErr = poDstGeometry.Transform( poCT );
                        if( eErr != 0 )
                        {
                            if( nGroupTransactions > 0)
                                poDstLayer.CommitTransaction();

                            System.err.println("Failed to reproject feature" + poFeature.GetFID() + " (geometry probably out of source or destination SRS).");
                            if( !bSkipFailures )
                            {
                                poFeature.delete();
                                poFeature = null;
                                poDstFeature.delete();
                                poDstFeature = null;
                                return false;
                            }
                        }
                    }
                    else if (poOutputSRS != null)
                    {
                        poDstGeometry.AssignSpatialReference(poOutputSRS);
                    }

                    if (poClipDst != null)
                    {
                        Geometry poClipped = poDstGeometry.Intersection(poClipDst);
                        if (poClipped == null || poClipped.IsEmpty())
                        {
                            /* Report progress */
                            nCount ++;
                            if (pfnProgress != null)
                                pfnProgress.run(nCount * 1.0 / nCountLayerFeatures, "");
View Full Code Here

{
    public static void main(String[] args)
    {
        FeatureDefn featureDefn = new FeatureDefn();

        Geometry point1 = new Geometry(ogr.wkbPoint);
        Geometry multipoint1 = new Geometry(ogr.wkbMultiPoint);
        multipoint1.AddGeometryDirectly(point1);
        try
        {
            /* Just to show that we are smart ! */
            multipoint1.AddGeometryDirectly(point1);
            System.err.println("should not reach that point");
        }
        catch(RuntimeException re)
        {
        }

        Geometry multipoint2 = new Geometry(ogr.wkbMultiPoint);
        multipoint2.AddGeometry(multipoint1.GetGeometryRef(0));
        try
        {
            /* Just to show that we are smart ! */
            multipoint2.AddGeometryDirectly(multipoint1.GetGeometryRef(0));
            System.err.println("should not reach that point");
        }
        catch(RuntimeException re)
        {
        }

        Geometry point3 = new Geometry(ogr.wkbPoint);
        new Feature(featureDefn).SetGeometryDirectly(point3);

        multipoint1 = null;

        for (int i = 0; i < 500000; i++)
        {
            if ((i % 100000) == 0) System.out.println(i);
            Feature feat = new Feature(featureDefn);
            feat.SetGeometryDirectly(new Geometry(ogr.wkbMultiPoint));
            feat.SetGeometry(null);
            feat.GetGeometryRef();
        }

        // Add features
        for (int i = 0; i < 1000000; i++)
        {
            if ((i % 100000) == 0) System.out.println(i);
            Feature feat = new Feature(featureDefn);
            feat.SetGeometry(new Geometry(ogr.wkbPoint));

            Geometry point = new Geometry(ogr.wkbPoint);
            Geometry multipoint = new Geometry(ogr.wkbMultiPoint);
            multipoint.AddGeometryDirectly(point);
        }

        /* Check that the objects are still alive despite their */
        /* Java containers and associated native objects */
        /* would have been finalized without a trick */
 
View Full Code Here

        public static void main(String[] args)
        {

            if (args.length != 1) usage();

            Geometry geom = Geometry.CreateFromWkt(args[0]);

            int wkbSize = geom.WkbSize();
            byte[] wkb = geom.ExportToWkb();
            if (wkb.length != wkbSize)
            {
                System.exit(-1);
            }
            if (wkbSize > 0)
            {
                System.out.print( "wkt-->wkb: ");
                for(int i=0;i<wkbSize;i++)
                {
                    if (i>0)
                        System.out.print("-");
                    int val = wkb[i];
                    if (val < 0)
                        val = 256 + val;
                    String hexVal = Integer.toHexString(val);
                    if (hexVal.length() == 1)
                        System.out.print("0");
                    System.out.print(hexVal);
                }
                System.out.print("\n");

                // wkb --> wkt (reverse test)
                Geometry geom2 = Geometry.CreateFromWkb(wkb);
                String geom_wkt = geom2.ExportToWkt();
                System.out.println( "wkb->wkt: " + geom_wkt );
            }

            // wkt -- gml transformation
            String gml = geom.ExportToGML();
            System.out.println( "wkt->gml: " + gml );

            Geometry geom3 = Geometry.CreateFromGML(gml);
            String geom_wkt2 = geom3.ExportToWkt();
            System.out.println( "gml->wkt: " + geom_wkt2 );
        }
View Full Code Here

TOP

Related Classes of org.gdal.ogr.Geometry

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.