1

I want to transform GeoJSON multipolygon to turf.polygon for find point within polygon but it can't read property from GeoJSON.

Is there any way to do that?

This is my polygon data in GeoJSON:

    var RG = {"type":"FeatureCollection",
"name":"RG_0",
"crs":{"type":"name","perties":{"name":"urn:ogc:def:crs:OGC:1.3:CRS84"}},
"features":[{
    "type":"Feature",
    "properties":{"OBJECTID":"1","LEFT_FID":"102","RIGHT_FID":"96",
    "Shape_Leng":17608.111100099999,
    "created_us":null,
    "created_da":null,
    "last_edite":null,
    "last_edi_1":null},
    "geometry":{
        "type":"MultiLineString"
        ,"coordinates"[ [ 107.74439, 15.13785 ],
                        [ 107.74435, 15.13789 ],
                        [ 107.74422, 15.13817 ],
                        [ 107.74406, 15.13853 ],
                        [ 107.74405, 15.13887 ],
                        [ 107.74396, 15.13959 ],
                        [ 107.74384, 15.14019 ],
                        [ 107.74361, 15.14078 ],
                        [ 107.74439, 15.13785 ]
                        [-46.653,-23.543],
                        [-46.634,-23.5346],
                        [ 107.4142, 14.4278],
                        [-46.613,-23.543],
                        [-46.614,-23.559],
                        [-46.631,-23.567],
                        [-46.653,-23.560],
                        [-46.653,-23.543]]

This is my code:

            var points = turf.points([
                [ -46.620,-23.545 ],
                [ 107.4142, 14.4278 ],
                [ 107.5644, 14.3850 ]
            ]);
            var searchWithin = turf.polygon(RG.features.geometry.coordinates);
            var ptsWithin = turf.pointsWithinPolygon(points, searchWithin);
            console.log(ptsWithin);

and this is my error:

OK.html:205 Uncaught TypeError: Cannot read property 'geometry' of undefined
2

There are several things that are wrong here:

  1. Syntax of coordinates definition in GeoJSON is not OK, but I suppose that just because of copying for question.
  2. features property of GeoJSON is an array, so you have to use index when refering to individual elements, in your case this would be RG.features[0].geometry.coordinates.
  3. Since in your case geometry is actually multipolygon, you must use turf.multiPolygon method to convert it to turf polygon.

So code should then look something like this:

var RG = {"type":"FeatureCollection",
"name":"RG_0",
"crs":{"type":"name","perties":{"name":"urn:ogc:def:crs:OGC:1.3:CRS84"}},
"features":[{
    "type":"Feature",
    "properties":{"OBJECTID":"1","LEFT_FID":"102","RIGHT_FID":"96",
    "Shape_Leng":17608.111100099999,
    "created_us":null,
    "created_da":null,
    "last_edite":null,
    "last_edi_1":null},
    "geometry": {
      "type": "MultiLinString",
      "coordinates": [
          [
            [
              [ 107.74439, 15.13785 ],
              [ 107.74435, 15.13789 ],
              [ 107.74422, 15.13817 ],
              [ 107.74406, 15.13853 ],
              [ 107.74405, 15.13887 ],
              [ 107.74396, 15.13959 ],
              [ 107.74384, 15.14019 ],
              [ 107.74361, 15.14078 ],
              [ 107.74439, 15.13785 ]
            ]
          ],
          [
            [
              [-46.653,-23.543],
              [-46.634,-23.5346],
              [ 107.4142, 14.4278],
              [-46.613,-23.543],
              [-46.614,-23.559],
              [-46.631,-23.567],
              [-46.653,-23.560],
              [-46.653,-23.543]
            ]
          ]
        ]
    }
}]};

var points = turf.points([
    [ -46.620,-23.545 ],
    [ 107.4142, 14.4278 ],
    [ 107.5644, 14.3850 ]
]);

var searchWithin = turf.multiPolygon(RG.features[0].geometry.coordinates);
var ptsWithin = turf.pointsWithinPolygon(points, searchWithin);
console.log(ptsWithin);

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.