Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
For example, if you have a GeoJSON file like this with a polygon(simple file for the test)
"type": "FeatureCollection",
"features": [
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
-4.658203125,
41.343824581185686
-5.6689453125,
39.13006024213511
-1.9335937499999998,
39.16414104768742
-1.3623046875,
41.21172151054787
-4.658203125,
41.343824581185686
The point:
Geometry point2 = new WKTReader().read("POINT (-3.2958984375 40.44694705960048)");
And you want to load the geoJSON file in your program to test in this polygon contains the point, how could you do it in Java using JTS?
Other option could be use GeoTools with GeoJson plugin but i'm not able to parse a GeoJson file
What I have tried
Using GEOTOOLS like this
String content = new String(Files.readAllBytes(Paths.get("file.geojson")), "UTF-8");
GeometryJSON gjson = new GeometryJSON();
Reader reader = new StringReader(content);
Polygon p = gjson.readPolygon(reader);
System.out.println("polygon: " + p);
The problem here is that polygon p only contains the last polygon of the geojson file. If this file have many polygons, how should I parse it?
Using JTS2GEOJSON like this
String content = new String(Files.readAllBytes(Paths.get("file.geojson")), "UTF-8");
System.out.println("content: " + content);
GeoJSONReader reader1 = new GeoJSONReader();
Geometry geometry = reader1.read(content);
This code fail is this line:
Geometry geometry = reader1.read(content);
With this error:
Exception in thread "main" java.lang.UnsupportedOperationException
at org.wololo.jts2geojson.GeoJSONReader.read(GeoJSONReader.java:51)
at org.wololo.jts2geojson.GeoJSONReader.read(GeoJSONReader.java:21)
at org.wololo.jts2geojson.GeoJSONReader.read(GeoJSONReader.java:16)
This error is due i'm trying to read a FeatureCollections from GeoJson file. It works if I tried with this simple string:
String content = "{\n" +
" \"type\": \"Polygon\",\n" +
" \"coordinates\": [\n" +
" [\n" +
" [\n" +
" -4.141845703125,\n" +
" 40.9218144123785\n" +
" ],\n" +
" [\n" +
" -4.603271484375,\n" +
" 40.002371935876475\n" +
" ],\n" +
" [\n" +
" -3.5595703125,\n" +
" 39.757879992021756\n" +
" ],\n" +
" [\n" +
" -2.548828125,\n" +
" 40.43858586704331\n" +
" ],\n" +
" [\n" +
" -3.2080078125,\n" +
" 41.12074559016745\n" +
" ],\n" +
" [\n" +
" -4.141845703125,\n" +
" 40.9218144123785\n" +
" ]\n" +
" ]\n" +
" ]\n" +
" }";
If you are using GeoTools then you need to start thinking in terms of DataStores and FeatureCollections. There is lots of built in smarts (much of it using JTS) to handle filtering etc which saves you a lot if potential issues.
File inFile = new File("/home/ian/Data/states/states.geojson");
Map<String, Object> params = new HashMap<>();
params.put(GeoJSONDataStoreFactory.URLP.key, URLs.fileToUrl(inFile));
DataStore newDataStore = DataStoreFinder.getDataStore(params);
String pt = "POINT (-107 42)";
FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
SimpleFeatureSource featureSource = newDataStore.getFeatureSource(newDataStore.getTypeNames()[0]);
Filter f = ff.contains(ff.property(featureSource.getSchema().getGeometryDescriptor().getLocalName()),
ff.literal(pt));
SimpleFeatureCollection collection = featureSource.getFeatures(f);
if (collection.size() > 0) {
try (SimpleFeatureIterator itr = collection.features()) {
while (itr.hasNext()) {
System.out.println(itr.next());
This will need the following in your pom.xml:
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geojsondatastore</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>${geotools.version}</version>
</dependency>
–
–
–
–
–
With the GeoJsonReader class you can read a GeoJson Geometry from a JSON fragment into a Geometry:
http://locationtech.github.io/jts/javadoc/org/locationtech/jts/io/geojson/GeoJsonReader.html
Then, test whether that geometry (geojson polygon) contains the argument geometry (wkt point):
http://locationtech.github.io/jts/javadoc/org/locationtech/jts/geom/Geometry.html#contains(org.locationtech.jts.geom.Geometry)
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.