1

I want to get the type (point, line, or polygon) of shapefile using GeoTools but so far I haven't found any code. Any other Java library also works for me.

  • I'll grant that the documentation is none too clear, but I think you should spend a little more time using the API before giving up. In a worst case you can fetch the first row, and use that to determine what type it is. – Vince Feb 1 '15 at 14:09
  • I thought about this as a workaround and I was wondering if there was a more direct way of doing it. Anyway thanks for your comment. – Ziliang Zhao Feb 1 '15 at 15:05
  • 1
    Get the feature type and check the geometry descriptor – Ian Turton Feb 1 '15 at 17:42
  • Thank you iant. Your solution works well and solves my issue. – Ziliang Zhao Feb 2 '15 at 8:05
4
DataStore dataStore = ....; // ShapefileDataStore
String t = dataStore.getTypeNames()[0];
SimpleFeatureSource featureSource = dataStore.getFeatureSource(t);
SimpleFeatureType schema = featureSource.getSchema();
String geomType = schema.getGeometryDescriptor().getType().getBinding().getName();
System.out.println(geomType);

It will out one of the following:

  • Point
  • Polygon
  • MultiPoint
  • MultiPolygon
  • LineString
  • MultiLineString
  • ...
  • what version of geotools, this works? for me always return MultiPolygon, same the object be a polygon... @dariush – Mateus Carvalho Nov 23 '17 at 21:28
  • geotools version 15.1 – Dariush Nov 24 '17 at 4:43
-1
     private Style createStyle2(FeatureSource featureSource) {
    SimpleFeatureType schema = (SimpleFeatureType)featureSource.getSchema();
    Class geomType = schema.getGeometryDescriptor().getType().getBinding();

    if (Polygon.class.isAssignableFrom(geomType)
            || MultiPolygon.class.isAssignableFrom(geomType)) {
         banderaelChapeesdePuntos = -1;
         banderaelChapeesdePoligonos = 1;
         banderaelChapeesdeLineas = -1;

        return createPolygonStyle();

    } else if (LineString.class.isAssignableFrom(geomType)
            || MultiLineString.class.isAssignableFrom(geomType)) {
         banderaelChapeesdePuntos = -1;
         banderaelChapeesdePoligonos = -1;
         banderaelChapeesdeLineas = 1;

        return createLineStyle();

    } else {
         banderaelChapeesdePuntos = 1;
         banderaelChapeesdePoligonos = -1;
         banderaelChapeesdeLineas = -1;
        return createPointStyle();
    }
}
  • 2
    please explain why this answers the question and how to implement it. – Brad Nesom Jun 25 '15 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.