Class FeatureUtilities


  • public class FeatureUtilities
    extends Object
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static double avg​(List<org.opengis.feature.simple.SimpleFeature> features, String field)
      Calculate the avg of a value in a list of SimpleFeatures.
      static boolean collectionToShapeFile​(String shapeFilePath, org.opengis.referencing.crs.CoordinateReferenceSystem crs, org.geotools.data.simple.SimpleFeatureCollection fet)
      The easy way to create a shapefile from attributes and geometries
      static org.geotools.data.simple.SimpleFeatureCollection createFeatureCollection​(org.opengis.feature.simple.SimpleFeature... features)
      Create a featurecollection from a vector of features
      static org.geotools.data.shapefile.ShapefileDataStore createShapeFileDatastore​(String name, String fieldsSpec, org.opengis.referencing.crs.CoordinateReferenceSystem crs)  
      static org.geotools.data.simple.SimpleFeatureCollection csvFileToFeatureCollection​(File csvFile, org.opengis.referencing.crs.CoordinateReferenceSystem crs, LinkedHashMap<String,​Integer> fieldsAndTypesIndex, String separator, IHMProgressMonitor pm)
      Convert a csv file to a FeatureCollection.
      static Collection<org.locationtech.jts.geom.Polygon> doVectorize​(org.geotools.coverage.grid.GridCoverage2D src, Map<String,​Object> args)
      Helper function to run the Vectorize operation with given parameters and retrieve the vectors.
      static org.locationtech.jts.geom.Polygon envelopeToPolygon​(org.geotools.geometry.Envelope2D envelope)
      Create a Polygon from an Envelope.
      static org.locationtech.jts.geom.Polygon envelopeToPolygon​(org.locationtech.jts.geom.Envelope envelope)
      Create a Polygon from an Envelope.
      static LinkedHashMap<String,​String> feature2AlphanumericToHashmap​(org.opengis.feature.simple.SimpleFeature feature)  
      static QueryResult featureCollection2QueryResult​(org.geotools.data.simple.SimpleFeatureCollection featureCollection)
      Utility to convert a featurecollection to a queryresult format.
      static String[] featureCollectionFieldNames​(org.geotools.data.simple.SimpleFeatureCollection feature)  
      static org.geotools.data.simple.SimpleFeatureCollection featureCollectionFromGeometry​(org.opengis.referencing.crs.CoordinateReferenceSystem crs, org.locationtech.jts.geom.Geometry... geometries)
      Make a SimpleFeatureCollection from a set of Geometry.
      static List<org.geotools.data.simple.SimpleFeatureCollection> featureCollectionsFromGeometry​(org.opengis.referencing.crs.CoordinateReferenceSystem crs, org.locationtech.jts.geom.Geometry... geometries)
      Make a set of SimpleFeatureCollection from a set of Geometry, based on the geometry type.
      static List<org.locationtech.jts.geom.Geometry> featureCollectionToGeometriesList​(org.geotools.data.simple.SimpleFeatureCollection collection, boolean doSubGeoms, String userDataField)
      Extracts features from a FeatureCollection into an ArrayList of its geometries.
      static List<org.opengis.feature.simple.SimpleFeature> featureCollectionToList​(org.geotools.data.simple.SimpleFeatureCollection collection)
      Extracts features from a FeatureCollection into an ArrayList.
      static List<FeatureMate> featureCollectionToMatesList​(org.geotools.data.simple.SimpleFeatureCollection collection)
      Extracts features from a FeatureCollection into an ArrayList of FeatureMates.
      static org.locationtech.jts.index.strtree.STRtree featureCollectionToSTRtree​(org.geotools.data.simple.SimpleFeatureCollection collection)
      Extracts features from a FeatureCollection into an STRtree.
      static String findAttributeName​(org.opengis.feature.simple.SimpleFeatureType featureType, String field)
      Find the name of an attribute, case insensitive.
      static Object getAttributeCaseChecked​(org.opengis.feature.simple.SimpleFeature feature, String field)
      Getter for attributes of a feature.
      static long getFeatureId​(org.opengis.feature.simple.SimpleFeature feature)
      Extract the numeric feature id from a feature.
      static double[][] histogram​(List<org.opengis.feature.simple.SimpleFeature> features, String field, int bins)
      Calculate the histogram of a list of SimpleFeatures.
      static org.locationtech.jts.geom.CoordinateList orderLineGeometries​(List<org.locationtech.jts.geom.Geometry> geometryList, double thresHold)
      Order the geometries of a list to be all directed in the same direction
      static org.opengis.feature.simple.SimpleFeature toDummyFeature​(org.locationtech.jts.geom.Geometry geom, org.opengis.referencing.crs.CoordinateReferenceSystem crs)  
    • Constructor Detail

      • FeatureUtilities

        public FeatureUtilities()
    • Method Detail

      • orderLineGeometries

        public static org.locationtech.jts.geom.CoordinateList orderLineGeometries​(List<org.locationtech.jts.geom.Geometry> geometryList,
                                                                                   double thresHold)
        Order the geometries of a list to be all directed in the same direction
        Parameters:
        geometryList - the list of geometries to be ordered
        thresHold - a scalar value that defines the max distance between two points to be the same
        Returns:
        a list of ordered coordinates
      • createFeatureCollection

        public static org.geotools.data.simple.SimpleFeatureCollection createFeatureCollection​(org.opengis.feature.simple.SimpleFeature... features)
        Create a featurecollection from a vector of features
        Parameters:
        features - - the vectore of features
        Returns:
        the created featurecollection
      • csvFileToFeatureCollection

        public static org.geotools.data.simple.SimpleFeatureCollection csvFileToFeatureCollection​(File csvFile,
                                                                                                  org.opengis.referencing.crs.CoordinateReferenceSystem crs,
                                                                                                  LinkedHashMap<String,​Integer> fieldsAndTypesIndex,
                                                                                                  String separator,
                                                                                                  IHMProgressMonitor pm)
                                                                                           throws Exception

        Convert a csv file to a FeatureCollection. This for now supports only point geometries.
        For different crs it also performs coor transformation.

        NOTE: this doesn't support date attributes

        Parameters:
        csvFile - the csv file.
        crs - the crs to use.
        fieldsAndTypes - the Map of filed names and types.
        pm - progress monitor.
        separatorthe - separator to use, if null, comma is used.
        Returns:
        the created FeatureCollection
        Throws:
        Exception
      • collectionToShapeFile

        public static boolean collectionToShapeFile​(String shapeFilePath,
                                                    org.opengis.referencing.crs.CoordinateReferenceSystem crs,
                                                    org.geotools.data.simple.SimpleFeatureCollection fet)
                                             throws IOException

        The easy way to create a shapefile from attributes and geometries

        NOTE: this doesn't support date attributes

        Parameters:
        shapeFilePath - the shapefile name
        crs - the destination crs
        fet - the featurecollection
        Throws:
        IOException
      • createShapeFileDatastore

        public static org.geotools.data.shapefile.ShapefileDataStore createShapeFileDatastore​(String name,
                                                                                              String fieldsSpec,
                                                                                              org.opengis.referencing.crs.CoordinateReferenceSystem crs)
                                                                                       throws Exception
        Parameters:
        name - the shapefile name
        fieldsSpec - to create other fields you can use a string like :
        "geom:MultiLineString,FieldName:java.lang.Integer"
        field name can not be over 10 characters use a ',' between each field
        field types can be : java.lang.Integer, java.lang.Long, // java.lang.Double, java.lang.String or java.util.Date
        Returns:
        Throws:
        Exception
      • featureCollectionToList

        public static List<org.opengis.feature.simple.SimpleFeature> featureCollectionToList​(org.geotools.data.simple.SimpleFeatureCollection collection)
        Extracts features from a FeatureCollection into an ArrayList.
        Parameters:
        collection - the feature collection.
        Returns:
        the list with the features or an empty list if no features present.
      • featureCollectionToSTRtree

        public static org.locationtech.jts.index.strtree.STRtree featureCollectionToSTRtree​(org.geotools.data.simple.SimpleFeatureCollection collection)
        Extracts features from a FeatureCollection into an STRtree.
        Parameters:
        collection - the feature collection.
        Returns:
        the tree containing the features.
      • featureCollectionToMatesList

        public static List<FeatureMate> featureCollectionToMatesList​(org.geotools.data.simple.SimpleFeatureCollection collection)
        Extracts features from a FeatureCollection into an ArrayList of FeatureMates.
        Parameters:
        collection - the feature collection.
        Returns:
        the list with the features or an empty list if no features present.
      • featureCollectionToGeometriesList

        public static List<org.locationtech.jts.geom.Geometry> featureCollectionToGeometriesList​(org.geotools.data.simple.SimpleFeatureCollection collection,
                                                                                                 boolean doSubGeoms,
                                                                                                 String userDataField)
        Extracts features from a FeatureCollection into an ArrayList of its geometries.
        Parameters:
        collection - the feature collection.
        doSubGeoms - split the geometries in single geometries (ex. MultiLines in Lines).
        userDataField - if not null, the data in the field are put in the userData field of the geometry.
        Returns:
        the list with the geometries or an empty list if no features present.
      • featureCollectionFromGeometry

        public static org.geotools.data.simple.SimpleFeatureCollection featureCollectionFromGeometry​(org.opengis.referencing.crs.CoordinateReferenceSystem crs,
                                                                                                     org.locationtech.jts.geom.Geometry... geometries)
        Make a SimpleFeatureCollection from a set of Geometry.

        This is a fast utility and adds no attributes.

        Parameters:
        crs - The CoordinateReferenceSystem.
        geometries - the set of Geometry to add.
        Returns:
        the features wrapping the geoms.
      • featureCollectionsFromGeometry

        public static List<org.geotools.data.simple.SimpleFeatureCollection> featureCollectionsFromGeometry​(org.opengis.referencing.crs.CoordinateReferenceSystem crs,
                                                                                                            org.locationtech.jts.geom.Geometry... geometries)
        Make a set of SimpleFeatureCollection from a set of Geometry, based on the geometry type.

        This is a fast utility and adds no attributes.

        Parameters:
        crs - The CoordinateReferenceSystem.
        geometries - the set of Geometry to add.
        Returns:
        the features wrapping the geoms.
      • getAttributeCaseChecked

        public static Object getAttributeCaseChecked​(org.opengis.feature.simple.SimpleFeature feature,
                                                     String field)
        Getter for attributes of a feature.

        If the attribute is not found, checks are done in non case sensitive mode.

        Parameters:
        feature - the feature from which to get the attribute.
        field - the name of the field.
        Returns:
        the attribute or null if none found.
      • findAttributeName

        public static String findAttributeName​(org.opengis.feature.simple.SimpleFeatureType featureType,
                                               String field)
        Find the name of an attribute, case insensitive.
        Parameters:
        featureType - the feature type to check.
        field - the case insensitive field name.
        Returns:
        the real name of the field, or null, if none found.
      • envelopeToPolygon

        public static org.locationtech.jts.geom.Polygon envelopeToPolygon​(org.geotools.geometry.Envelope2D envelope)
        Create a Polygon from an Envelope.
        Parameters:
        envelope - the envelope to convert.
        Returns:
        the created polygon.
      • envelopeToPolygon

        public static org.locationtech.jts.geom.Polygon envelopeToPolygon​(org.locationtech.jts.geom.Envelope envelope)
        Create a Polygon from an Envelope.
        Parameters:
        envelope - the envelope to convert.
        Returns:
        the created polygon.
      • doVectorize

        public static Collection<org.locationtech.jts.geom.Polygon> doVectorize​(org.geotools.coverage.grid.GridCoverage2D src,
                                                                                Map<String,​Object> args)
        Helper function to run the Vectorize operation with given parameters and retrieve the vectors.
        Parameters:
        src - the source GridCoverage2D.
        args - a Map of parameter names and values or null.
        Returns:
        the generated vectors as JTS Polygons
      • toDummyFeature

        public static org.opengis.feature.simple.SimpleFeature toDummyFeature​(org.locationtech.jts.geom.Geometry geom,
                                                                              org.opengis.referencing.crs.CoordinateReferenceSystem crs)
      • avg

        public static double avg​(List<org.opengis.feature.simple.SimpleFeature> features,
                                 String field)
        Calculate the avg of a value in a list of SimpleFeatures.

        Empty records are ignored.

        Parameters:
        features - the features.
        field - the field to consider.
        Returns:
        the avg.
      • histogram

        public static double[][] histogram​(List<org.opengis.feature.simple.SimpleFeature> features,
                                           String field,
                                           int bins)
        Calculate the histogram of a list of SimpleFeatures.
        Parameters:
        features - the list of features.
        field - the field to consider.
        bins - the number of bins.
        Returns:
        the histogram as matrix of rows num like bins and 3 columns for [binCenter, count, cumulated-normalize-count].
      • feature2AlphanumericToHashmap

        public static LinkedHashMap<String,​String> feature2AlphanumericToHashmap​(org.opengis.feature.simple.SimpleFeature feature)
      • featureCollectionFieldNames

        public static String[] featureCollectionFieldNames​(org.geotools.data.simple.SimpleFeatureCollection feature)
      • getFeatureId

        public static long getFeatureId​(org.opengis.feature.simple.SimpleFeature feature)
        Extract the numeric feature id from a feature.
        Parameters:
        feature - the feature to check.
        Returns:
        the id as long.
      • featureCollection2QueryResult

        public static QueryResult featureCollection2QueryResult​(org.geotools.data.simple.SimpleFeatureCollection featureCollection)
        Utility to convert a featurecollection to a queryresult format.
        Parameters:
        featureCollection - the collection to convert.
        Returns:
        the queryresult object.