I want to know the true type of Geometry using GeoTools (point, polygon, multipolygon). GeoTools always returns me as a MultiPolygon, and my object has this format for example:

"geometry": {"type": "polygon", "coordinates": [-86.577085000372449, 34.793410999917739], [-86.57692100000412, 34.793837999823893], [-86.576509999936491, 34.794884000396337]

Currently I have the following code:

Map <String, Object> map = new HashMap <> ();
        map.put ("url", file.toURI (). toURL ());

        DataStore dataStore = DataStoreFinder.getDataStore (map);
        String typeName = dataStore.getTypeNames () [0];

        FeatureSource <SimpleFeatureType, SimpleFeature> source = dataStore
                .getFeatureSource (typeName);

        FeatureCollection <SimpleFeatureType, SimpleFeature> collection = source.getFeatures ();
        try (FeatureIterator <SimpleFeature> features = collection.features ()) {
            while (features.hasNext ()) {
                SimpleFeature feature = features.next ();
               feature.getAttribute ("the_geom"). toString ();

Because Shapefiles can only handle one type of geometry if any of your features contains a multipolygon the geometry column needs to be of type multipolygon.

You can check if a multipolygon is really a simple polygon by using the getNumGeometries method.

try (FeatureIterator<SimpleFeature> features = collection.features()) {
  while (features.hasNext()) {
    SimpleFeature feature = features.next();
    MultiPolygon geom = (MultiPolygon) feature.getDefaultGeometry();
    String type = "Polygon";
    if(geom.getNumGeometries()>1) {
      type = "MultiPolygon";
    System.out.println(feature.getAttribute("STATE_NAME")+" "+type);

As a stylistic note you should use the URLs utility class to convert your file to a URL otherwise you run the risk of failures in windows environments.

File file = new File("/home/ian/Data/states/states.shp");
map.put("url", URLs.fileToUrl(file));

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.