Package org.gdal.ogr

Examples of org.gdal.ogr.FeatureDefn


        /*
         * Invoke.
         */

        FeatureDefn feature = layer.GetLayerDefn();
       
        gdal.ContourGenerate(band, contourInterval, offset, fixedLevelsDouble,
                (ignoreNodata ? 1 : 0), sourceNodata, layer, feature.GetFieldIndex("ID"),
                (attributName != null ? feature.GetFieldIndex(attributName) : -1),
                progressCallback);
       
        dataSource.delete();
        dataset.delete();
    }
View Full Code Here


    static FeatureDefn featureDefn;
    static Object mutex = new Object();

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

        new Thread() {
            public void run()
            {
                while(!bHasFinished)
View Full Code Here

      {
         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. */
            /* -------------------------------------------------------------------- */
            if( poFeatureDefn == null )
            {
               poFeatureDefn = CloneFeatureDefn(poLayer.GetLayerDefn()); // XXX: no Clone supported in java binding!!
            }
            else if ( !accept_different_schemas )
            {
               FeatureDefn poFeatureDefnCur = poLayer.GetLayerDefn();
               assert(null != poFeatureDefnCur);

               int fieldCount = poFeatureDefnCur.GetFieldCount();

               if( fieldCount != poFeatureDefn.GetFieldCount())
               {
                  System.err.print("Number of attributes of layer "+poLayer.GetLayerDefn().GetName()+" of "+args[nFirstSourceDataset]+" does not match ... skipping it.\n");

                  if (bFirstWarningForNonMatchingAttributes)
                  {
                     System.err.print("Note : you can override this behaviour with -accept_different_schemas option\n"
                           + "but this may result in a tileindex incompatible with MapServer\n");
                     bFirstWarningForNonMatchingAttributes = false;
                  }
                  continue;
               }

               boolean bSkip = false;
               for( int fn = 0; fn < poFeatureDefnCur.GetFieldCount(); fn++ )
               {
                  FieldDefn poField = poFeatureDefn.GetFieldDefn(fn);
                  FieldDefn poFieldCur = poFeatureDefnCur.GetFieldDefn(fn);

                  /* XXX - Should those pointers be checked against null? */
                  assert(null != poField);
                  assert(null != poFieldCur);

View Full Code Here

   }

    /* Adhoc method to workaround the lack of a FeatureDefn.Clone() method */
    static FeatureDefn CloneFeatureDefn(FeatureDefn poSrcFeatureDefn)
    {
        FeatureDefn poFeatureDefn = new FeatureDefn(poSrcFeatureDefn.GetName());
        poFeatureDefn.SetGeomType(poSrcFeatureDefn.GetGeomType());
        for(int fi = 0; fi < poSrcFeatureDefn.GetFieldCount(); fi++)
            poFeatureDefn.AddFieldDefn(poSrcFeatureDefn.GetFieldDefn(fi));
        return poFeatureDefn;
    }
View Full Code Here

                            String pszWHERE,
                            ProgressCallback pfnProgress)
   
    {
        Layer    poDstLayer;
        FeatureDefn poSrcFDefn;
        int      eErr;
        boolean         bForceToPolygon = false;
        boolean         bForceToMultiPolygon = false;
        boolean         bForceToMultiLineString = false;
   
        if( pszNewLayerName == null )
            pszNewLayerName = poSrcLayer.GetLayerDefn().GetName();
   
        if( wkbFlatten(eGType) == ogr.wkbPolygon )
            bForceToPolygon = true;
        else if( wkbFlatten(eGType) == ogr.wkbMultiPolygon )
            bForceToMultiPolygon = true;
        else if( wkbFlatten(eGType) == ogr.wkbMultiLineString )
            bForceToMultiLineString = true;
   
    /* -------------------------------------------------------------------- */
    /*      Setup coordinate transformation if we need it.                  */
    /* -------------------------------------------------------------------- */
        CoordinateTransformation poCT = null;
   
        if( bTransform )
        {
            if( poSourceSRS == null )
                poSourceSRS = poSrcLayer.GetSpatialRef();
   
            if( poSourceSRS == null )
            {
                System.err.println("Can't transform coordinates, source layer has no\n" +
                        "coordinate system.  Use -s_srs to set one." );
                System.exit( 1 );
            }
   
            /*CPLAssert( null != poSourceSRS );
            CPLAssert( null != poOutputSRS );*/
   
            /* New in GDAL 1.10. Before was "new CoordinateTransformation(srs,dst)". */
            poCT = CoordinateTransformation.CreateCoordinateTransformation( poSourceSRS, poOutputSRS );
            if( poCT == null )
            {
                String pszWKT = null;
   
                System.err.println("Failed to create coordinate transformation between the\n" +
                    "following coordinate systems.  This may be because they\n" +
                    "are not transformable, or because projection services\n" +
                    "(PROJ.4 DLL/.so) could not be loaded." );
               
                pszWKT = poSourceSRS.ExportToPrettyWkt( 0 );
                System.err.println( "Source:\n" + pszWKT );
               
                pszWKT = poOutputSRS.ExportToPrettyWkt( 0 );
                System.err.println( "Target:\n" + pszWKT );
                System.exit( 1 );
            }
        }
       
    /* -------------------------------------------------------------------- */
    /*      Get other info.                                                 */
    /* -------------------------------------------------------------------- */
        poSrcFDefn = poSrcLayer.GetLayerDefn();
       
        if( poOutputSRS == null )
            poOutputSRS = poSrcLayer.GetSpatialRef();
   
    /* -------------------------------------------------------------------- */
    /*      Find the layer.                                                 */
    /* -------------------------------------------------------------------- */

        /* GetLayerByName() can instanciate layers that would have been */
        /* 'hidden' otherwise, for example, non-spatial tables in a */
        /* Postgis-enabled database, so this apparently useless command is */
        /* not useless... (#4012) */
        gdal.PushErrorHandler("CPLQuietErrorHandler");
        poDstLayer = poDstDS.GetLayerByName(pszNewLayerName);
        gdal.PopErrorHandler();
        gdal.ErrorReset();

        int iLayer = -1;
        if( poDstLayer != null )
        {
            int nLayerCount = poDstDS.GetLayerCount();
            for( iLayer = 0; iLayer < nLayerCount; iLayer++ )
            {
                Layer        poLayer = poDstDS.GetLayer(iLayer);

                if( poLayer != null
                    && poLayer.GetName().equals(poDstLayer.GetName()) )
                {
                    break;
                }
            }

            if (iLayer == nLayerCount)
                /* shouldn't happen with an ideal driver */
                poDstLayer = null;
        }

    /* -------------------------------------------------------------------- */
    /*      If the user requested overwrite, and we have the layer in       */
    /*      question we need to delete it now so it will get recreated      */
    /*      (overwritten).                                                  */
    /* -------------------------------------------------------------------- */
        if( poDstLayer != null && bOverwrite )
        {
            if( poDstDS.DeleteLayer( iLayer ) != 0 )
            {
                System.err.println(
                        "DeleteLayer() failed when overwrite requested." );
                return false;
            }
            poDstLayer = null;
        }
   
    /* -------------------------------------------------------------------- */
    /*      If the layer does not exist, then create it.                    */
    /* -------------------------------------------------------------------- */
        if( poDstLayer == null )
        {
            if( eGType == -2 )
            {
                eGType = poSrcFDefn.GetGeomType();

                if ( bExplodeCollections )
                {
                    int n25DBit = eGType & ogr.wkb25DBit;
                    if (wkbFlatten(eGType) == ogr.wkbMultiPoint)
                    {
                        eGType = ogr.wkbPoint | n25DBit;
                    }
                    else if (wkbFlatten(eGType) == ogr.wkbMultiLineString)
                    {
                        eGType = ogr.wkbLineString | n25DBit;
                    }
                    else if (wkbFlatten(eGType) == ogr.wkbMultiPolygon)
                    {
                        eGType = ogr.wkbPolygon | n25DBit;
                    }
                    else if (wkbFlatten(eGType) == ogr.wkbGeometryCollection)
                    {
                        eGType = ogr.wkbUnknown | n25DBit;
                    }
                }

                if ( pszZField != null )
                    eGType |= ogr.wkb25DBit;
            }
   
            if( poDstDS.TestCapability( ogr.ODsCCreateLayer ) == false)
            {
                System.err.println(
                "Layer " + pszNewLayerName + "not found, and CreateLayer not supported by driver.");
                return false;
            }
   
            gdal.ErrorReset();
   
            poDstLayer = poDstDS.CreateLayer( pszNewLayerName, poOutputSRS,
                                              eGType, papszLCO );
   
            if( poDstLayer == null )
                return false;
   
            bAppend = false;
        }
   
    /* -------------------------------------------------------------------- */
    /*      Otherwise we will append to it, if append was requested.        */
    /* -------------------------------------------------------------------- */
        else if( !bAppend )
        {
            System.err.println("FAILED: Layer " + pszNewLayerName + "already exists, and -append not specified.\n" +
                                "        Consider using -append, or -overwrite.");
            return false;
        }
        else
        {
            if( papszLCO.size() > 0 )
            {
                System.err.println("WARNING: Layer creation options ignored since an existing layer is\n" +
                        "         being appended to." );
            }
        }
   
    /* -------------------------------------------------------------------- */
    /*      Add fields.  Default to copy all field.                         */
    /*      If only a subset of all fields requested, then output only      */
    /*      the selected fields, and in the order that they were            */
    /*      selected.                                                       */
    /* -------------------------------------------------------------------- */
        int         iField;

        /* Initialize the index-to-index map to -1's */
        int nSrcFieldCount = poSrcFDefn.GetFieldCount();
        int[] panMap = new int [nSrcFieldCount];
        for( iField=0; iField < nSrcFieldCount; iField++)
            panMap[iField] = -1;

        FeatureDefn poDstFDefn = poDstLayer.GetLayerDefn();

        if (papszSelFields != null && !bAppend )
        {
            int  nDstFieldCount = 0;
            if (poDstFDefn != null)
                nDstFieldCount = poDstFDefn.GetFieldCount();

            for( iField=0; iField < papszSelFields.size(); iField++)
            {
                int iSrcField = poSrcFDefn.GetFieldIndex((String)papszSelFields.get(iField));
                if (iSrcField >= 0)
                {
                    FieldDefn poSrcFieldDefn = poSrcFDefn.GetFieldDefn(iSrcField);
                    FieldDefn oFieldDefn = new FieldDefn( poSrcFieldDefn.GetNameRef(),
                                                poSrcFieldDefn.GetFieldType() );
                    oFieldDefn.SetWidth( poSrcFieldDefn.GetWidth() );
                    oFieldDefn.SetPrecision( poSrcFieldDefn.GetPrecision() );

                    if (papszFieldTypesToString != null &&
                        (CSLFindString(papszFieldTypesToString, "All") != -1 ||
                        CSLFindString(papszFieldTypesToString,
                                    ogr.GetFieldTypeName(poSrcFDefn.GetFieldDefn(iSrcField).GetFieldType())) != -1))
                        oFieldDefn.SetType(ogr.OFTString);

                    /* The field may have been already created at layer creation */
                    int iDstField = -1;
                    if (poDstFDefn != null)
                        iDstField = poDstFDefn.GetFieldIndex(oFieldDefn.GetNameRef());
                    if (iDstField >= 0)
                    {
                        panMap[iSrcField] = iDstField;
                    }
                    else if (poDstLayer.CreateField( oFieldDefn ) == 0)
                    {
                        /* now that we've created a field, GetLayerDefn() won't return NULL */
                        if (poDstFDefn == null)
                            poDstFDefn = poDstLayer.GetLayerDefn();

                        /* Sanity check : if it fails, the driver is buggy */
                        if (poDstFDefn != null &&
                            poDstFDefn.GetFieldCount() != nDstFieldCount + 1)
                        {
                            System.err.println(
                                    "The output driver has claimed to have added the " + oFieldDefn.GetNameRef() + " field, but it did not!");
                        }
                        else
                        {
                            panMap[iSrcField] = nDstFieldCount;
                            nDstFieldCount ++;
                        }
                    }

                }
                else
                {
                    System.err.println("Field '" + (String)papszSelFields.get(iField) + "' not found in source layer.");
                        if( !bSkipFailures )
                            return false;
                }
            }

            /* -------------------------------------------------------------------- */
            /* Use SetIgnoredFields() on source layer if available                  */
            /* -------------------------------------------------------------------- */

            /* Here we differ from the ogr2ogr.cpp implementation since the OGRFeatureQuery */
            /* isn't mapped to swig. So in that case just don't use SetIgnoredFields() */
            /* to avoid issue raised in #4015 */
            if (poSrcLayer.TestCapability(ogr.OLCIgnoreFields) && pszWHERE == null)
            {
                int iSrcField;
                Vector papszIgnoredFields = new Vector();
                for(iSrcField=0;iSrcField<nSrcFieldCount;iSrcField++)
                {
                    String pszFieldName =
                        poSrcFDefn.GetFieldDefn(iSrcField).GetNameRef();
                    boolean bFieldRequested = false;
                    for( iField=0; iField < papszSelFields.size(); iField++)
                    {
                        if (pszFieldName.equalsIgnoreCase((String)papszSelFields.get(iField)))
                        {
                            bFieldRequested = true;
                            break;
                        }
                    }

                    if (pszZField != null && pszFieldName.equalsIgnoreCase(pszZField))
                        bFieldRequested = true;

                    /* If source field not requested, add it to ignored files list */
                    if (!bFieldRequested)
                        papszIgnoredFields.addElement(pszFieldName);
                }
                poSrcLayer.SetIgnoredFields(papszIgnoredFields);
            }
        }
        else if( !bAppend )
        {
            int nDstFieldCount = 0;
            if (poDstFDefn != null)
                nDstFieldCount = poDstFDefn.GetFieldCount();
            for( iField = 0; iField < nSrcFieldCount; iField++ )
            {
                FieldDefn poSrcFieldDefn = poSrcFDefn.GetFieldDefn(iField);
                FieldDefn oFieldDefn = new FieldDefn( poSrcFieldDefn.GetNameRef(),
                                            poSrcFieldDefn.GetFieldType() );
                oFieldDefn.SetWidth( poSrcFieldDefn.GetWidth() );
                oFieldDefn.SetPrecision( poSrcFieldDefn.GetPrecision() );

                if (papszFieldTypesToString != null &&
                    (CSLFindString(papszFieldTypesToString, "All") != -1 ||
                    CSLFindString(papszFieldTypesToString,
                                ogr.GetFieldTypeName(poSrcFDefn.GetFieldDefn(iField).GetFieldType())) != -1))
                    oFieldDefn.SetType(ogr.OFTString);

                /* The field may have been already created at layer creation */
                int iDstField = -1;
                if (poDstFDefn != null)
                    iDstField = poDstFDefn.GetFieldIndex(oFieldDefn.GetNameRef());
                if (iDstField >= 0)
                {
                    panMap[iField] = iDstField;
                }
                else if (poDstLayer.CreateField( oFieldDefn ) == 0)
                {
                    /* now that we've created a field, GetLayerDefn() won't return NULL */
                    if (poDstFDefn == null)
                        poDstFDefn = poDstLayer.GetLayerDefn();

                    /* Sanity check : if it fails, the driver is buggy */
                    if (poDstFDefn != null &&
                        poDstFDefn.GetFieldCount() != nDstFieldCount + 1)
                    {
                        System.err.println(
                                "The output driver has claimed to have added the " + oFieldDefn.GetNameRef() + " field, but it did not!");
                    }
                    else
                    {
                        panMap[iField] = nDstFieldCount;
                        nDstFieldCount ++;
                    }
                }
            }
        }
        else
        {
            /* For an existing layer, build the map by fetching the index in the destination */
            /* layer for each source field */
            if (poDstFDefn == null)
            {
                System.err.println("poDstFDefn == NULL.\n" );
                return false;
            }

            for( iField = 0; iField < nSrcFieldCount; iField++ )
            {
                FieldDefn poSrcFieldDefn = poSrcFDefn.GetFieldDefn(iField);
                int iDstField = poDstFDefn.GetFieldIndex(poSrcFieldDefn.GetNameRef());
                if (iDstField >= 0)
                    panMap[iField] = iDstField;
            }
        }
   
View Full Code Here

public class OGRTestGC
{
    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
View Full Code Here

TOP

Related Classes of org.gdal.ogr.FeatureDefn

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.