Skip to content

Instantly share code, notes, and snippets.

@ateucher
Last active June 28, 2016 02:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ateucher/59365353d61ab41bf7ae14905941eaab to your computer and use it in GitHub Desktop.
Save ateucher/59365353d61ab41bf7ae14905941eaab to your computer and use it in GitHub Desktop.
rgdal::readOGR / GDAL 2.1.0 bug test
Display the source blob
Display the rendered blob
Raw
{"type":"FeatureCollection","features":[
{"type":"Feature","geometry":{"type":"Polygon","coordinates":
[[[102,2],[102,3],[103,3],[103,2],[102,2]]]},"properties":{"a":0,"id":0}},
{"type":"Feature","geometry":{"type":"Polygon","coordinates":
[[[100,0],[100,1],[101,1],[101,0],[100,0]]]},"properties":{"a":1,"id":0}}
]}
library(rgdal)
#> Loading required package: sp
#> rgdal: version: 1.1-10, (SVN revision 622)
#> Geospatial Data Abstraction Library extensions to R successfully loaded
#> Loaded GDAL runtime: GDAL 2.1.0, released 2016/04/25
#> Path to GDAL shared files: /usr/share/gdal/2.1
#> Loaded PROJ.4 runtime: Rel. 4.9.2, 08 September 2015, [PJ_VERSION: 492]
#> Path to PROJ.4 shared files: (autodetected)
#> Linking to sp version: 1.2-3
## GeoJSON with two polygons with same id:
polys_dup_ids <- '{"type":"FeatureCollection","features":[
{"type":"Feature","geometry":{"type":"Polygon","coordinates":
[[[102,2],[102,3],[103,3],[103,2],[102,2]]]},"properties":{"a":0,"id":0}},
{"type":"Feature","geometry":{"type":"Polygon","coordinates":
[[[100,0],[100,1],[101,1],[101,0],[100,0]]]},"properties":{"a":1,"id":0}}
]}'
## One of the polygons gets dropped
spdf <- readOGR(polys_dup_ids, "OGRGeoJSON")
length(spdf@polygons)
#> [1] 1
## Also happens with disambiguateFIDs = TRUE
spdf <- readOGR(polys_dup_ids, "OGRGeoJSON", disambiguateFIDs = TRUE)
length(spdf@polygons)
#> [1] 1
## GeoJSON with two polygons with no id property, and unique other fields:
polys_no_dup_ids <- '{"type":"FeatureCollection","features":[
{"type":"Feature","geometry":{"type":"Polygon","coordinates":
[[[102,2],[102,3],[103,3],[103,2],[102,2]]]},"properties":{"a":0}},
{"type":"Feature","geometry":{"type":"Polygon","coordinates":
[[[100,0],[100,1],[101,1],[101,0],[100,0]]]},"properties":{"a":1}}
]}'
## This works as expected
spdf <- readOGR(polys_no_dup_ids, "OGRGeoJSON")
length(spdf)
#> [1] 2
## GeoJSON with two polygons with no id property, duplicate other fields:
polys_dup_fields <- '{"type":"FeatureCollection","features":[
{"type":"Feature","geometry":{"type":"Polygon","coordinates":
[[[102,2],[102,3],[103,3],[103,2],[102,2]]]},"properties":{"a":0}},
{"type":"Feature","geometry":{"type":"Polygon","coordinates":
[[[100,0],[100,1],[101,1],[101,0],[100,0]]]},"properties":{"a":0}}
]}'
## This works as expected
spdf <- readOGR(polys_dup_fields, "OGRGeoJSON")
length(spdf)
#> [1] 2
sessionInfo()
#> R version 3.3.1 (2016-06-21)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 16.04 LTS
#>
#> locale:
#> [1] LC_CTYPE=en_CA.UTF-8 LC_NUMERIC=C
#> [3] LC_TIME=en_CA.UTF-8 LC_COLLATE=en_CA.UTF-8
#> [5] LC_MONETARY=en_CA.UTF-8 LC_MESSAGES=en_CA.UTF-8
#> [7] LC_PAPER=en_CA.UTF-8 LC_NAME=C
#> [9] LC_ADDRESS=C LC_TELEPHONE=C
#> [11] LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] rgdal_1.1-10 sp_1.2-3
#>
#> loaded via a namespace (and not attached):
#> [1] tools_3.3.1 grid_3.3.1 lattice_0.20-33
ogrinfo -al '{"type":"FeatureCollection","features":[
{"type":"Feature","geometry":{"type":"Polygon","coordinates":
[[[102,2],[102,3],[103,3],[103,2],[102,2]]]},"properties":{"a":0,"id":0}},
{"type":"Feature","geometry":{"type":"Polygon","coordinates":
[[[100,0],[100,1],[101,1],[101,0],[100,0]]]},"properties":{"a":1,"id":0}}
]}'
# INFO: Open of `test.geojson'
# using driver `GeoJSON' successful.
#
# Layer name: OGRGeoJSON
# Geometry: Polygon
# Feature Count: 1
# Extent: (100.000000, 0.000000) - (101.000000, 1.000000)
# Layer SRS WKT:
# GEOGCS["WGS 84",
# DATUM["WGS_1984",
# SPHEROID["WGS 84",6378137,298.257223563,
# AUTHORITY["EPSG","7030"]],
# AUTHORITY["EPSG","6326"]],
# PRIMEM["Greenwich",0,
# AUTHORITY["EPSG","8901"]],
# UNIT["degree",0.0174532925199433,
# AUTHORITY["EPSG","9122"]],
# AUTHORITY["EPSG","4326"]]
# FID Column = id
# a: Integer (0.0)
# id: Integer (0.0)
# OGRFeature(OGRGeoJSON):0
# a (Integer) = 1
# id (Integer) = 0
# POLYGON ((100 0,100 1,101 1,101 0,100 0))
@ateucher
Copy link
Author

Looks like it was fixed in GDAL 2.1.1: https://trac.osgeo.org/gdal/ticket/6538

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment