2

I have this GeoJSON file with the coordinates of neighborhoods in Valencia (Spain). Then I am using GeoTools to load the file and check if a given coordinate is inside the GeoJSON file and if YES retrieve the unique id of that region. I created my Java program based on this answer. It is compiling but I cannot find if my point is inside a region of the GeoJSON file.

{
  "type": "FeatureCollection",
  "name": "boundary_administrative_Valencia_lines",
  "crs": {
    "type": "name",
    "properties": {
      "name": "urn:ogc:def:crs:OGC:1.3:CRS84"
    }
  },
  "features": [
    {
      "type": "Feature",
      "properties": {
        "full_id": "w4786833",
        "osm_id": "4786833",
        "osm_type": "way",
        "admin_level": "4",
        "boundary": "administrative",
        "natural": "coastline",
        "surface": "",
        "name": "",
        "border_type": "",
        "highway": "",
        "name:ca": "",
        "name:es": "",
        "name:lt": "",
        "wikidata": "",
        "wikipedia": "",
        "source:date": "",
        "ref": "",
        "name:it": "",
        "bridge": "",
        "layer": "",
        "man_made": "",
        "name:en": ""
      },
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [
            0.1778961,
            38.8127748
          ],
          [
            0.1777836,
            38.8128318
          ],
          [
            0.1777124,
            38.8129247
          ],

My Java program:

public class TestGeoJson {
    public static void main(String[] args) {
        try {
            File inFile = new File("resources/valencia/boundary_administrative_Valencia_lines.geojson");
            if (!inFile.exists()) {
                return;
            }
            Map<String, Object> params = new HashMap<String, Object>();
            params.put(GeoJSONDataStoreFactory.URLP.key, URLs.fileToUrl(inFile));
            DataStore newDataStore = DataStoreFinder.getDataStore(params);

            String pt = "LineString (0.1778961 38.8127748)";

            FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
            SimpleFeatureSource featureSource = newDataStore.getFeatureSource(newDataStore.getTypeNames()[0]);
            Contains contains = ff.contains(
                    ff.property(featureSource.getSchema().getGeometryDescriptor().getLocalName()), ff.literal(pt));
            SimpleFeatureCollection collection = featureSource.getFeatures(contains);
            if (collection.size() > 0) {
                try (SimpleFeatureIterator itr = collection.features()) {
                    while (itr.hasNext()) {
                        System.out.println(itr.next());
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
1

You will never (well hardly ever) find a point within the objects contained in your JSON file because they are lines not polygons so they have no inside for your point to be in.

You will never get an answer even when you fetch some regions as your point is too far to the SE. But with Point(-1.1778961 39.4127748) you do get an hit.

  • ok, should I usea GeoJSON file which has this content "geometry": {"type": "Point", "coordinates": [-0.1550775, 38.996095] } or this content "geometry": {"type": "MultiPolygon","coordinates": [[[[-0.2727165,39.2784496],....? – Felipe Jul 26 at 8:41
  • 1
    if you want to find which area contains your point you need the polygons. – Ian Turton Jul 26 at 8:43
  • thanks. I will look for this file. – Felipe Jul 26 at 9:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.