5

I am new to R and geospatial data analysis. I would like to extract the coordinates from a .geojson file in R. For example, a line from the .geojson file is as follows:

{ "type": "Feature", "properties": { "id": 36084.000000, "osm_id": 471120302.000000, "type": "tertiary", "name": null, "tunnel": 0, "bridge": 0, "oneway": 0, "ref": null, "z_order": 4.000000, "access": null, "service": null, "class": "highway" }, "geometry": { "type": "LineString", "coordinates": [ [ 117.152985627930335, 39.199392977596688 ], [ 117.154272752981669, 39.197016121313759 ], [ 117.156671150755756, 39.193602255970234 ] ] } }

I used 'readOGR' to read the file.

data <- readOGR(dsn = "test.geojson", layer = "OGRGeoJSON")

I could extract features like osm_id using data[['osm_id']], but that does not work with coordinates. How to extract the coordinates? Can anyone give me a hint how to proceed?

2
  • Use coordinates(data)
    – rcs
    Feb 2, 2017 at 8:45
  • That might work for points but for polygons it gives you the centroid and for lines I have no idea what it gives you...
    – Spacedman
    Feb 3, 2017 at 17:52

2 Answers 2

2

You could try the geojsonio package, which i've used for a few tasks involving sp/sf conversions.

# read in the geojson data and specify that you want to parse it
data <- geojson_read("C:\\filepath\\test.geojson", method = "local", parse = TRUE)

It has a strange old listed structure after parsing, here's my dummy polygon:

$type
[1] "FeatureCollection"

$crs
$crs$type
[1] "name"

$crs$properties
$crs$properties$name
[1] "urn:ogc:def:crs:OGC:1.3:CRS84"


$features
     type id dummy geometry.type
1 Feature  1     0       Polygon

geometry.coordinates
1 16.4844, 22.5928, 24.7461, 17.4951, 16.4844, 59.7363, 61.1426, 55.0342, 55.1221, 59.7363

you can see the area of interest; list item named "features".

data$features is actually a data.frame, but the 4th column (so to speak) is another data.frame consisting of a character and a list. The list, our coordinates, is in fact an array. It's all rather nested.

# this accesses the geometry
data$features$geometry
 type
1 Polygon

coordinates
1 16.4844, 22.5928, 24.7461, 17.4951, 16.4844, 59.7363, 61.1426, 55.0342, 55.1221, 59.7363

# and this accesses the list in which the array in which the coordinates are stored
data$features$geometry$coordinates[[1]]

, , 1

        [,1]    [,2]    [,3]    [,4]    [,5]
[1,] 16.4844 22.5928 24.7461 17.4951 16.4844

, , 2

        [,1]    [,2]    [,3]    [,4]    [,5]
[1,] 59.7363 61.1426 55.0342 55.1221 59.7363

So there we go. To access your coords, you can either call the entire array as a matrix, the individual coord pairs or all of the x/y coordinates:

# all coordinate pairs as matrix
data$features$geometry$coordinates[[1]][1,,]

# individual coordinate pairs (i've put 1:5 here illustratively, which is the same as above)
data$features$geometry$coordinates[[1]][,1:5,]

# all x or y coordinates (dont use word or!)
data$features$geometry$coordinates[[1]][,,1 or 2]
0

Converting the data to a fortified tidy dataframe does the trick. See below:

mydf_fortified <- tidy(mydf) #mydg is a geojson file

The ids in the mydf_fortified dataframe will correspond to the relevant list items, you can then join to a new data frame if needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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