Skip to content

Instantly share code, notes, and snippets.

@ateucher
Created October 23, 2015 22:39
Show Gist options
  • Save ateucher/c1b0cb32bfa3c2c860ff to your computer and use it in GitHub Desktop.
Save ateucher/c1b0cb32bfa3c2c860ff to your computer and use it in GitHub Desktop.
library(rgdal)
poly <- '{ "type": "Polygon",
"coordinates": [
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]
]
}'
poly2 <- '{ "type": "FeatureCollection",
"features": [
{ "type": "Feature", "id": 0, "properties": { "FID": 0 },
"geometry": { "type": "Polygon", "coordinates": [
[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]
]}},
{ "type": "Feature", "id": 1, "properties": { "FID": 1 },
"geometry": { "type": "Polygon", "coordinates": [
[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]
] } } ] }'
poly_with_hole <- '{ "type": "Polygon",
"coordinates": [
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ],
[ [100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2] ]
]
}'
multipoly <- '{ "type": "MultiPolygon",
"coordinates": [
[[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]],
[[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]]
]
}'
multipoly_with_hole <- '{ "type": "MultiPolygon",
"coordinates": [
[[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]],
[[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]],
[[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]]
]
}'
spdf <- readOGR(poly_with_hole, layer='OGRGeoJSON', verbose=FALSE)
temp <- tempfile()
writeOGR(spdf, dsn = temp, layer = "", driver="GeoJSON")
cat(readLines(temp))
spdf2 <- readOGR(temp, layer = 'OGRGeoJSON', verbose = FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment