Arun Kumar
Arun Kumar

Reputation: 6794

GeoJSON : Check if a Geo Coordinate is inside Polygon

I have following GeoJSON data file which contains a polygon with coordinates.

[
    {
        "geometry": {
        "type": "Polygon",
        "coordinates": 
[
        [
          [
            9.137248,
            48.790411
          ],
          [
            9.137248,
            48.790263
          ],
          [
            9.13695,
            48.790263
          ],
          [
            9.137248,
            48.790411
          ]
        ]
      ]
    }
    }
  ]

With help of org.geotools.geojson.geom.GeometryJSON , I am parsing the JSON coordinates in com.vividsolutions.jts.geom.Polygon class as given below. And also checking if Coordinate(9.13710, 48.790360) is inside this polygon or not.

GeometryJSON g = new GeometryJSON();
         com.vividsolutions.jts.geom.Polygon polygon = g.readPolygon(new File(fileLocation));
         System.out.println("Type="+polygon.getGeometryType());

         GeometryFactory gf = new GeometryFactory();
         boolean pointIsInPolygon = polygon.contains(gf.createPoint(new Coordinate(9.13710, 48.790360)));
         System.out.println("Point is in polygon="+pointIsInPolygon);

But my program always give me below result that given coordinate is not in Polygon.

Result

Type=Polygon

Point is in polygon=false

a) Could you see why pointIsInPolygon is false. Am I missing something?

b) What Cordinate i should give here so make pointIsInPolygon result in true

c )is there any other way to parse the given JSON file in Polygon and confirm that if a coordinate lies in Polygon or not?

Upvotes: 2

Views: 6123

Answers (1)

Ian Turton
Ian Turton

Reputation: 10976

Your point is outside the polygon so the result is correct. Here the red point is yours, if you change it to be gf.createPoint(new Coordinate(9.13716433655009652, 48.79030985534630815)); (the green point) it returns true.

enter image description here

Upvotes: 8

Related Questions