Package org.gdal.osr

Examples of org.gdal.osr.SpatialReference


        /*
         * Try to get a coordinate system from the raster.
         */

        SpatialReference srs = null;

        String wkt = dataset.GetProjection();

        if (wkt.length() > 0) {

            srs = new SpatialReference(wkt);
        }

        /*
         * Create the outputfile.
         */
 
View Full Code Here


    {
        gdal.AllRegister();
        Driver memDriver = gdal.GetDriverByName("MEM");
        Dataset ds1 = memDriver.Create("mem", 100, 100);
        ds1.SetGeoTransform(new double[] { 2, 0.01, 0, 49, 0, -0.01 });
        SpatialReference sr1 = new SpatialReference();
        sr1.ImportFromEPSG(4326);

        Dataset ds2 = memDriver.Create("mem2", 100, 100);
        ds2.SetGeoTransform(new double[] { 400000, 1000, 0, 5500000, 0, -1000 });
        SpatialReference sr2 = new SpatialReference();
        sr2.ImportFromEPSG(32631);

        Vector options = new Vector();
        options.add("SRC_SRS=" + sr1.ExportToWkt());
        options.add("DST_SRS=" + sr2.ExportToWkt());
        Transformer t = new Transformer(ds1, ds2, options);
        double argout[] = new double[3];
        argout[0] = 0;
        argout[1] = 0;
        int ret = t.TransformPoint(0, argout);
View Full Code Here

            if( nFirstSourceDataset < args.length-2 && args[nFirstSourceDataset].charAt(0) == '-' )
            {
               nFirstSourceDataset++;
            }

            SpatialReference poSrcSpatialRef = null;

            /* Fetches the SRS of the first layer and use it when creating the tileindex layer */
            if (nFirstSourceDataset < args.length)
            {
               DataSource poDS = ogr.Open( args[nFirstSourceDataset],false );

               if (poDS!=null)
               {
                  for(int iLayer = 0; iLayer < poDS.GetLayerCount(); iLayer++ )
                  {
                     boolean bRequested = bLayersWildcarded;
                     Layer poLayer = poDS.GetLayer(iLayer);

                     for(int iArg = 0; iArg < args.length && !bRequested; iArg++ )
                     {
                        if( args[iArg].equalsIgnoreCase("-lnum")
                              && Integer.parseInt(args[iArg+1]) == iLayer )
                           bRequested = true;
                        else if( args[iArg].equalsIgnoreCase("-lname")
                              && args[iArg+1].equalsIgnoreCase(poLayer.GetLayerDefn().GetName()) )
                           bRequested = true;
                     }

                     if( !bRequested )
                        continue;

                     if ( poLayer.GetSpatialRef() != null)
                        poSrcSpatialRef = poLayer.GetSpatialRef().Clone();
                     break;
                  }
               }

               poDS.delete();
            }

            poDstLayer = poDstDS.CreateLayer( "tileindex", poSrcSpatialRef );
            poDstLayer.CreateField( oLocation, ogr.OFTString );

            /* with the OGR Java bindings, avoid using the delete() methods,
             * except on the datasource objects, where it is necessary to close properly the
             * native file handles.
             */ 
            // poSrcSpatialRef.delete();
         }
      }

      /* -------------------------------------------------------------------- */
      /*      Identify target layer and field.                                */
      /* -------------------------------------------------------------------- */
      int   iTileIndexField;

      poDstLayer = poDstDS.GetLayer(0);
      if( poDstLayer == null )
      {
         System.err.print("Can't find any layer in output tileindex!\n" );
         return;
      }

      iTileIndexField =
         poDstLayer.GetLayerDefn().GetFieldIndex( pszTileIndexField );
      if( iTileIndexField == -1 )
      {
         System.err.print("Can't find "+pszTileIndexField+" field in tile index dataset.\n");
         return;
      }

      FeatureDefn poFeatureDefn = null;

      /* Load in memory existing file names in SHP */
      int nExistingLayers = 0;
      String[] existingLayersTab = null;
      SpatialReference alreadyExistingSpatialRef = null;
      boolean alreadyExistingSpatialRefValid = false;
      nExistingLayers = poDstLayer.GetFeatureCount();
      if (nExistingLayers > 0)
      {
         existingLayersTab = new String[nExistingLayers];
         for(int i=0;i<nExistingLayers;i++)
         {
            Feature feature = poDstLayer.GetNextFeature();
            existingLayersTab[i] = feature.GetFieldAsString( iTileIndexField);
            if (i == 0)
            {
               DataSource       poDS;
               String filename = existingLayersTab[i];
               int j;
               for(j=filename.length()-1;j>=0;j--)
               {
                  if (filename.charAt(j) == ',')
                     break;
               }
               if (j >= 0)
               {
                  int iLayer = Integer.parseInt(filename.substring(j + 1));
                  filename = filename.substring(0, j);
                  poDS = ogr.Open(filename,false );
                  if (poDS!=null)
                  {
                     Layer poLayer = poDS.GetLayer(iLayer);
                     if (poLayer!=null)
                     {
                        alreadyExistingSpatialRefValid = true;
                        alreadyExistingSpatialRef =
                           (poLayer.GetSpatialRef()!=null) ? poLayer.GetSpatialRef().Clone() : null;

                           if (poFeatureDefn == null) {
                              poFeatureDefn = CloneFeatureDefn(poLayer.GetLayerDefn()); // XXX: no Clone supported in java binding!!
                           }
                     }
                     poDS.delete();
                  }
               }
            }
         }
      }

      /* ignore check */
      //if (write_absolute_path)
      //{
      //   current_path = CPLGetCurrentDir();
      //   if (current_path == null)
      //   {
      //      fprintf( stderr, "This system does not support the CPLGetCurrentDir call. "
      //      "The option -write_absolute_path will have no effect\n");
      //      write_absolute_path = false;
      //   }
      //}
      /* ==================================================================== */
      /*      Process each input datasource in turn.                          */
      /* ==================================================================== */

      for(; nFirstSourceDataset < args.length; nFirstSourceDataset++ )
      {
         DataSource       poDS;

         if( args[nFirstSourceDataset].charAt(0) == '-' )
         {
            nFirstSourceDataset++;
            continue;
         }

         String fileNameToWrite;

         //VSIStatBuf sStatBuf;
         // FIXME: handle absolute path check
         //if (write_absolute_path && CPLIsFilenameRelative( args[nFirstSourceDataset] ) &&
         //      VSIStat( args[nFirstSourceDataset], &sStatBuf ) == 0)
         //{
         //   fileNameToWrite = CPLStrdup(CPLProjectRelativeFilename(current_path,args[nFirstSourceDataset]));
         //}
         //else
         //{
         //   fileNameToWrite = args[nFirstSourceDataset];
         //}
         fileNameToWrite = args[nFirstSourceDataset];

         poDS = ogr.Open( args[nFirstSourceDataset], false );

         if( poDS == null )
         {
            System.err.print("Failed to open dataset "+args[nFirstSourceDataset]+", skipping.\n");
            continue;
         }

         /* -------------------------------------------------------------------- */
         /*      Check all layers, and see if they match requests.               */
         /* -------------------------------------------------------------------- */
         for(int iLayer = 0; iLayer < poDS.GetLayerCount(); iLayer++ )
         {
            boolean bRequested = bLayersWildcarded;
            Layer poLayer = poDS.GetLayer(iLayer);

            for(int iArg = 0; iArg < args.length && !bRequested; iArg++ )
            {
               if( args[iArg].equalsIgnoreCase("-lnum")
                     && Integer.parseInt(args[iArg+1]) == iLayer )
                  bRequested = true;
               else if( args[iArg].equalsIgnoreCase("-lname")
                     && args[iArg+1].equalsIgnoreCase(poLayer.GetLayerDefn().GetName()) )
                  bRequested = true;
            }

            if( !bRequested )
               continue;

            /* Checks that the layer is not already in tileindex */
            int i;
            for(i=0;i<nExistingLayers;i++)
            {
               String szLocation = fileNameToWrite+","+iLayer;
               if (szLocation.equalsIgnoreCase(existingLayersTab[i]))
               {
                  System.err.println("Layer "+iLayer+" of "+args[nFirstSourceDataset]+" is already in tileindex. Skipping it.\n");
                  break;
               }
            }
            if (i != nExistingLayers)
            {
               continue;
            }

            SpatialReference spatialRef = poLayer.GetSpatialRef();
            if (alreadyExistingSpatialRefValid)
            {
               if ((spatialRef != null && alreadyExistingSpatialRef != null &&
                     spatialRef.IsSame(alreadyExistingSpatialRef) == 0) ||
                     ((spatialRef != null) != (alreadyExistingSpatialRef != null)))
               {
                  System.err.print("Warning : layer "+iLayer+" of "+args[nFirstSourceDataset]+" is not using the same projection system as "
                        + "other files in the tileindex. This may cause problems when "
                        + "using it in MapServer for example."+((skip_different_projection) ? " Skipping it" : "")+"\n");
                  ;
                  if (skip_different_projection)
                  {
                     continue;
                  }
               }
            }
            else
            {
               alreadyExistingSpatialRefValid = true;
               alreadyExistingSpatialRef = (spatialRef!=null) ? spatialRef.Clone() : null;
            }

            /* -------------------------------------------------------------------- */
            /*    Check if all layers in dataset have the same attributes  schema. */
            /* -------------------------------------------------------------------- */
 
View Full Code Here

                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: "
                                + "-clipsrc must be used with -spat option or \n"
                                + "a bounding box, WKT string or datasource must be specified");
                Usage();
            }
        }

        /*
         * Find the output driver.
         */

        Driver driver = gdal.GetDriverByName(outputFormat);

        if (driver == null) {

            System.out.println("FAILURE: Output driver '" + outputFormat
                    + "' not recognized.");
            Usage();
        }

        /*
         * Open input datasource.
         */

        DataSource srcDS = ogr.Open(sourceFilename);

        if (srcDS == null) {

            System.out.println("Unable to open input datasource '"
                    + sourceFilename);
            System.out.println(gdal.GetLastErrorMsg());
            System.exit(3);
        }

        /*
         * Create target raster file.
         */

        Dataset dstDS = null;
        String[] layerList = layers.split(":");
        int layerCount = layerList.length;
        int bandCount = layerCount;

        if (SQL != null) {
            bandCount++;
        }

        if (sizeX == 0) {
            sizeX = 256;
        }

        if (sizeY == 0) {
            sizeY = 256;
        }

        String[] optionList = createOptions == null ? null : createOptions
                .split(":");

        dstDS = driver.Create(outputFilename, sizeX, sizeY, 1, outputType,
                optionList);

        if (dstDS == null) {

            System.out.println("Unable to create dataset '" + outputFilename);
            System.out.println(gdal.GetLastErrorMsg());
            System.exit(3);
        }

        /*
         * Use terminal progress report
         */
        if (quiet == false) {

            progressCallback = new TermProgressCallback();
        }

        /*
         * Process SQL request.
         */

        if (SQL != null) {

            Layer srcLayer = srcDS.ExecuteSQL(SQL);

            if (srcLayer != null) {

                ProcessLayer(srcLayer, dstDS, clipSrc, sizeX, sizeY, 1,
                        isXExtentSet, isYExtentSet, minX, maxX, minY, maxY,
                        burnAttribute, outputType, algorithmAndOptions, quiet,
                        progressCallback);
            }
        }

        /*
         * Process each layer.
         */

        for (int i = 0; i < layerList.length; i++) {

            Layer srcLayer = srcDS.GetLayerByName(layerList[i]);

            if (srcLayer == null) {

                System.out.println("Unable to find layer '" + layerList[i]);
                continue;
            }

            if (where != null) {

                if (srcLayer.SetAttributeFilter(where) != gdalconstConstants.CE_None) {
                    break;
                }
            }

            if (spatialFilter != null) {

                srcLayer.SetSpatialFilter(spatialFilter);
            }

            if (outputSRS == null) {

                SpatialReference srs = srcLayer.GetSpatialRef();

                if (srs != null) {

                    outputSRS = srs.ExportToWkt();
                }
            }

            ProcessLayer(srcLayer, dstDS, clipSrc, sizeX, sizeY, i + 1
                    + bandCount - layerCount, isXExtentSet, isYExtentSet, minX,
View Full Code Here

    try
    {
      /* -------------------------------------------------------------------- */
      /*      Initialize srs                                                  */
      /* -------------------------------------------------------------------- */
      SpatialReference src = new SpatialReference("");
      src.ImportFromProj4("+proj=latlong +datum=WGS84 +no_defs");
      System.out.println( "SOURCE IsGeographic:" + src.IsGeographic() + " IsProjected:" + src.IsProjected() );
      SpatialReference dst = new SpatialReference("");
      dst.ImportFromProj4("+proj=somerc +lat_0=47.14439372222222 +lon_0=19.04857177777778 +x_0=650000 +y_0=200000 +ellps=GRS67 +units=m +no_defs");
      System.out.println( "DEST IsGeographic:" + dst.IsGeographic() + " IsProjected:" + dst.IsProjected() );
      /* -------------------------------------------------------------------- */
      /*      making the transform                                            */
      /* -------------------------------------------------------------------- */
            /* New in GDAL 1.10. Before was "new CoordinateTransformation(srs,dst)". */
      CoordinateTransformation ct = CoordinateTransformation.CreateCoordinateTransformation(src, dst);
View Full Code Here

      /* -------------------------------------------------------------------- */
      /*      Report projection.                                              */
      /* -------------------------------------------------------------------- */
      if (hDataset.GetProjectionRef() != null) {
        SpatialReference hSRS;
        String pszProjection;

        pszProjection = hDataset.GetProjectionRef();

        hSRS = new SpatialReference(pszProjection);
        if (hSRS != null && pszProjection.length() != 0) {
          String[] pszPrettyWkt = new String[1];

          hSRS.ExportToPrettyWkt(pszPrettyWkt, 0);
          System.out.println("Coordinate System is:");
          System.out.println(pszPrettyWkt[0]);
          //gdal.CPLFree( pszPrettyWkt );
        } else
          System.out.println("Coordinate System is `"
              + hDataset.GetProjectionRef() + "'");

        hSRS.delete();
      }

      /* -------------------------------------------------------------------- */
      /*      Report Geotransform.                                            */
      /* -------------------------------------------------------------------- */
 
View Full Code Here

    /* -------------------------------------------------------------------- */
    /*      Setup transformation to lat/long.                               */
    /* -------------------------------------------------------------------- */
    if (pszProjection != null && pszProjection.length() > 0) {
      SpatialReference hProj, hLatLong = null;

      hProj = new SpatialReference(pszProjection);
      if (hProj != null)
        hLatLong = hProj.CloneGeogCS();

      if (hLatLong != null) {
        /* New in GDAL 1.10. Before was "new CoordinateTransformation(srs,dst)". */
        hTransform = CoordinateTransformation.CreateCoordinateTransformation(hProj, hLatLong);
            }

      if (hProj != null)
        hProj.delete();
    }

    /* -------------------------------------------------------------------- */
    /*      Transform to latlong and report.                                */
    /* -------------------------------------------------------------------- */
 
View Full Code Here

        Vector papszDSCO = new Vector(), papszLCO = new Vector();
        boolean bTransform = false;
        boolean bAppend = false, bUpdate = false, bOverwrite = false;
        String pszOutputSRSDef = null;
        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") ) )
                {
                    poClipDst = Geometry.CreateFromWkt(args[iArg+1]);
                    if (poClipDst == 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
                {
                    pszClipDstDS = args[iArg+1];
                    iArg ++;
                }
            }
            else if( args[iArg].equalsIgnoreCase("-clipdstsql") && iArg < args.length-1 )
            {
                pszClipDstSQL = args[iArg+1];
                iArg ++;
            }
            else if( args[iArg].equalsIgnoreCase("-clipdstlayer") && iArg < args.length-1 )
            {
                pszClipDstLayer = args[iArg+1];
                iArg ++;
            }
            else if( args[iArg].equalsIgnoreCase("-clipdstwhere") && iArg < args.length-1 )
            {
                pszClipDstWhere = args[iArg+1];
                iArg ++;
            }
            else if( args[iArg].equalsIgnoreCase("-explodecollections") )
            {
                bExplodeCollections = true;
            }
            else if( args[iArg].equalsIgnoreCase("-zfield") && iArg < args.length-1 )
            {
                pszZField = args[iArg+1];
                iArg ++;
            }
            else if( args[iArg].charAt(0) == '-' )
            {
                Usage();
            }
            else if( pszDestDataSource == null )
                pszDestDataSource = args[iArg];
            else if( pszDataSource == null )
                pszDataSource = args[iArg];
            else
                papszLayers.addElement (args[iArg] );
        }
   
        if( pszDataSource == null )
            Usage();

        if( bPreserveFID && bExplodeCollections )
        {
            System.err.print("FAILURE: cannot use -preserve_fid and -explodecollections at the same time\n\n" );
            Usage();
        }

        if( bClipSrc && pszClipSrcDS != null)
        {
            poClipSrc = LoadGeometry(pszClipSrcDS, pszClipSrcSQL, pszClipSrcLayer, pszClipSrcWhere);
            if (poClipSrc == null)
            {
                System.err.print("FAILURE: cannot load source clip geometry\n\n" );
                Usage();
            }
        }
        else if( bClipSrc && poClipSrc == null )
        {
            if (poSpatialFilter != null)
                poClipSrc = poSpatialFilter.Clone();
            if (poClipSrc == null)
            {
                System.err.print("FAILURE: -clipsrc must be used with -spat option or a\n" +
                                "bounding box, WKT string or datasource must be specified\n\n");
                Usage();
            }
        }
       
        if( pszClipDstDS != null)
        {
            poClipDst = LoadGeometry(pszClipDstDS, pszClipDstSQL, pszClipDstLayer, pszClipDstWhere);
            if (poClipDst == null)
            {
                System.err.print("FAILURE: cannot load dest clip geometry\n\n" );
                Usage();
            }
        }
    /* -------------------------------------------------------------------- */
    /*      Open data source.                                               */
    /* -------------------------------------------------------------------- */
        DataSource poDS;
           
        poDS = ogr.Open( pszDataSource, false );
   
    /* -------------------------------------------------------------------- */
    /*      Report failure                                                  */
    /* -------------------------------------------------------------------- */
        if( poDS == null )
        {
            System.err.println("FAILURE:\n" +
                    "Unable to open datasource ` " + pszDataSource + "' with the following drivers.");
   
            for( int iDriver = 0; iDriver < ogr.GetDriverCount(); iDriver++ )
            {
                System.err.println("  . " + ogr.GetDriver(iDriver).GetName() );
            }
   
            System.exit( 1 );
        }
   
    /* -------------------------------------------------------------------- */
    /*      Try opening the output datasource as an existing, writable      */
    /* -------------------------------------------------------------------- */
        DataSource       poODS = null;
        Driver poDriver = null;
       
        if( bUpdate )
        {
            poODS = ogr.Open( pszDestDataSource, true );
            if( poODS == null )
            {
                if (bOverwrite || bAppend)
                {
                    poODS = ogr.Open( pszDestDataSource, false );
                    if ( poODS == null )
                    {
                        /* ok the datasource doesn't exist at all */
                        bUpdate = false;
                    }
                    else
                    {
                        poODS.delete();
                        poODS = null;
                    }
                }

                if (bUpdate)
                {
                    System.err.println("FAILURE:\n" +
                            "Unable to open existing output datasource `" + pszDestDataSource + "'.");
                    System.exit( 1 );
                }
            }
   
            else if( papszDSCO.size() > 0 )
            {
                System.err.println("WARNING: Datasource creation options ignored since an existing datasource\n" +
                        "         being updated." );
            }

            if (poODS != null)
                poDriver = poODS.GetDriver();
        }
   
    /* -------------------------------------------------------------------- */
    /*      Find the output driver.                                         */
    /* -------------------------------------------------------------------- */
        if( !bUpdate )
        {
            int                  iDriver;
   
            poDriver = ogr.GetDriverByName(pszFormat);
            if( poDriver == null )
            {
                System.err.println("Unable to find driver `" + pszFormat +"'." );
                System.err.println( "The following drivers are available:" );
           
                for( iDriver = 0; iDriver < ogr.GetDriverCount(); iDriver++ )
                {
                    System.err.println("  . " + ogr.GetDriver(iDriver).GetName() );
                }
                System.exit( 1 );
            }
   
            if( poDriver.TestCapability( ogr.ODrCCreateDataSource ) == false )
            {
                System.err.println( pszFormat + " driver does not support data source creation.");
                System.exit( 1 );
            }

    /* -------------------------------------------------------------------- */
    /*      Special case to improve user experience when translating        */
    /*      a datasource with multiple layers into a shapefile. If the      */
    /*      user gives a target datasource with .shp and it does not exist, */
    /*      the shapefile driver will try to create a file, but this is not */
    /*      appropriate because here we have several layers, so create      */
    /*      a directory instead.                                            */
    /* -------------------------------------------------------------------- */
            if (poDriver.GetName().equalsIgnoreCase("ESRI Shapefile") &&
                pszSQLStatement == null &&
                (papszLayers.size() > 1 ||
                 (papszLayers.size() == 0 && poDS.GetLayerCount() > 1)) &&
                pszNewLayerName == null &&
                (pszDestDataSource.endsWith(".shp") || pszDestDataSource.endsWith(".SHP")))
            {
                File f = new File(pszDestDataSource);
                if (!f.exists())
                {
                    if (!f.mkdir())
                    {
                        System.err.println(
                            "Failed to create directory " + pszDestDataSource + "\n" +
                            "for shapefile datastore.");
                        System.exit(1);
                    }
                }
            }

    /* -------------------------------------------------------------------- */
    /*      Create the output data source.                                  */
    /* -------------------------------------------------------------------- */
            poODS = poDriver.CreateDataSource( pszDestDataSource, papszDSCO );
            if( poODS == null )
            {
                System.err.println( pszFormat + " driver failed to create "+ pszDestDataSource );
                System.exit( 1 );
            }
        }
   
    /* -------------------------------------------------------------------- */
    /*      Parse the output SRS definition if possible.                    */
    /* -------------------------------------------------------------------- */
        if( pszOutputSRSDef != null )
        {
            poOutputSRS = new SpatialReference();
            if( poOutputSRS.SetFromUserInput( pszOutputSRSDef ) != 0 )
            {
                System.err.println( "Failed to process SRS definition: " + pszOutputSRSDef );
                System.exit( 1 );
            }
        }
   
    /* -------------------------------------------------------------------- */
    /*      Parse the source SRS definition if possible.                    */
    /* -------------------------------------------------------------------- */
        if( pszSourceSRSDef != null )
        {
            poSourceSRS = new SpatialReference();
            if( poSourceSRS.SetFromUserInput( pszSourceSRSDef ) != 0 )
            {
                System.err.println( "Failed to process SRS definition: " + pszSourceSRSDef );
                System.exit( 1 );
            }
        }
View Full Code Here

        ((SpatialReference)spatialRef).delete();
    }
   
    @Override
    public Object NewSpatialRef(String wkt) {
        return new SpatialReference(wkt);
    }
View Full Code Here

TOP

Related Classes of org.gdal.osr.SpatialReference

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.