private SimpleFeatureType createFeatureType(@Nonnull final String geojsonData) {
try {
JSONObject geojson = new JSONObject(geojsonData);
if (geojson.has("type") && geojson.getString("type").equalsIgnoreCase("FeatureCollection")) {
CoordinateReferenceSystem crs = parseCoordinateReferenceSystem(geojson);
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.setName("GeosjonFeatureType");
final JSONArray features = geojson.getJSONArray("features");
Set<String> allAttributes = Sets.newHashSet();
Class<Geometry> geomType = null;
for (int i = 0; i < features.length(); i++) {
final JSONObject feature = features.getJSONObject(i);
final JSONObject properties = feature.getJSONObject("properties");
final Iterator keys = properties.keys();
while (keys.hasNext()) {
String nextKey = (String) keys.next();
if (!allAttributes.contains(nextKey)) {
allAttributes.add(nextKey);
builder.add(nextKey, Object.class);
}
if (geomType != Geometry.class) {
Class<Geometry> thisGeomType = parseGeometryType(feature);
if (thisGeomType != null) {
if (geomType == null) {
geomType = thisGeomType;
} else if (geomType != thisGeomType) {
geomType = Geometry.class;
}
}
}
}
}
builder.add("geometry", geomType, crs);
builder.setDefaultGeometry("geometry");
return builder.buildFeatureType();
} else {
return null;
}
} catch (JSONException e) {
throw new PrintException("Invalid geoJSON: \n" + geojsonData + ": " + e.getMessage(), e);