[GIS] Check Geometry Type of a Shapefile Using GeoTools

geotoolsshapefile

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.

Best Answer

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
  • ...
Related Question