More

Check Geometry Type of a Shapefile Using GeoTools?


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.


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

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(); } }


Watch the video: Join CSV to SHP QGIS (October 2021).