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.– VinceFeb 1, 2015 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 ZhaoFeb 1, 2015 at 15:05
-
1Get the feature type and check the geometry descriptor– Ian Turton ♦Feb 1, 2015 at 17:42
-
Thank you iant. Your solution works well and solves my issue.– Ziliang ZhaoFeb 2, 2015 at 8:05
2 Answers
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 Nov 23, 2017 at 21:28
-
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();
}
}
-
2please explain why this answers the question and how to implement it. Jun 25, 2015 at 19:22