0

EDIT: Turns out fromJSON() has a variable called simplifyVector that by default goes TRUE. By setting it to FALSE I was able to turn the string into Json, from Json into geoJson and from geoJson into spatialpolygonsdataframe. Still don't know why it happens, though.

I have these two polygons in geoJSON format I got from an API and I want to export them to shp. When I read these two polygons using fromJSON() I get two different formats. Apparently, the reason would be because the second polygon includes a third coordinate in some of its vertex. Why is it happening?

It's worth noting that I've been successfully able to export the polygons to shp in the second format of the reproducible example below, but I haven't had the same success with the first format. How could I "standardize" them so I could read multiple polygons in these two possible formats, and export them to shp files?

Here's a reproducible example:

Polygon1 <- '{"type":"Polygon","coordinates":[[[-70.3248,-27.3708],[-70.3247,-27.3713],[-70.3246,-27.3719],[-70.3245,-27.3723],[-70.3239,-27.3743],[-70.3198,-27.374],[-70.3194,-27.3724],[-70.3193,-27.3719],[-70.3196,-27.3704],[-70.3201,-27.3696],[-70.3204,-27.3695],[-70.3222,-27.3693],[-70.3248,-27.3708]]]}'

Polygon2 <- '{"type":"Polygon","coordinates":[[[-70.597,-33.4169,0.0],[-70.5952,-33.4144],[-70.5947,-33.4139],[-70.5926,-33.4133],[-70.5921,-33.4133],[-70.5907,-33.4138],[-70.59,-33.4142],[-70.5885,-33.4152],[-70.5894,-33.4163],[-70.5916,-33.4195],[-70.5918,-33.4197],[-70.5929,-33.4191],[-70.5943,-33.4184],[-70.5955,-33.4177],[-70.597,-33.4169,0.0]]]}' Geojson1 <- fromJSON(Polygon1) print(Geojson1) Geojson2 <- fromJSON(Polygon2) print(Geojson2)

And the output of the above code:

print(Geojson1) $type [1] "Polygon" $coordinates , , 1 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] -70.3248 -70.3247 -70.3246 -70.3245 -70.3239 -70.3198 -70.3194 -70.3193 -70.3196 -70.3201 [,11] [,12] [,13] [1,] -70.3204 -70.3222 -70.3248 , , 2 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] -27.3708 -27.3713 -27.3719 -27.3723 -27.3743 -27.374 -27.3724 -27.3719 -27.3704 -27.3696 [,11] [,12] [,13] [1,] -27.3695 -27.3693 -27.3708

print(Geojson2) $type [1] "Polygon" $coordinates $coordinates[[1]] $coordinates[[1]][[1]] [1] -70.5970 -33.4169 0.0000 $coordinates[[1]][[2]] [1] -70.5952 -33.4144 $coordinates[[1]][[3]] [1] -70.5947 -33.4139 $coordinates[[1]][[4]] [1] -70.5926 -33.4133 $coordinates[[1]][[5]] [1] -70.5921 -33.4133 $coordinates[[1]][[6]] [1] -70.5907 -33.4138 $coordinates[[1]][[7]] [1] -70.5900 -33.4142 $coordinates[[1]][[8]] [1] -70.5885 -33.4152 $coordinates[[1]][[9]] [1] -70.5894 -33.4163 $coordinates[[1]][[10]] [1] -70.5916 -33.4195 $coordinates[[1]][[11]] [1] -70.5918 -33.4197 $coordinates[[1]][[12]] [1] -70.5929 -33.4191 $coordinates[[1]][[13]] [1] -70.5943 -33.4184 $coordinates[[1]][[14]] [1] -70.5955 -33.4177 $coordinates[[1]][[15]] [1] -70.5970 -33.4169 0.0000

  • Maybe try rjson, I find the combinations of simplify in jsonlite make it unuseable because there are incompatible outputs depending on when matching-dimension assumptions are met. You can have no simplify but then every coordinate single value is in its own list, which is hard to interpret. I haven't explored this enough to understand it fully, but rjson might work for what you want. – mdsumner Jan 28 '17 at 7:06
1

If the task is to create a shapefile , you could write to file and use sf::st_read("file.geojson"); then use st_write to write out (rows that all have the same geometry type) to one shapefile.

I believe the direct answer to the question is that in the first, the simplify logic detects that a 2-coord*13 can be an array, and perhaps the outer "[" is detected as a degenerate dimension (see that it's 3D 1x2x13, or maybe 2x1x13, and not 2x13). In the second, the first and last also have a 3-coord, so that array simplify logic does not kick in.

You'll want to find out why those start and end coords are XYZ but the rest are XY. (And if that's "normal" then I really want to find out why too).

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.