[R-sig-Geo] Convert geojson file to R

Edzer Pebesma edzer@pebe@m@ @end|ng |rom un|-muen@ter@de
Tue Nov 29 12:34:37 CET 2022


Interestingly, what seems to works is

readLines('countrymasks.geojson') |> st_read() -> r

with a warning:

Warning message:
In readLines("countrymasks.geojson") :
   incomplete final line found on 'countrymasks.geojson'


On 29/11/2022 00:58, Miluji Sb wrote:
> Thank you. I will report this bug (I did not have the confidence to call
> this a bug before).
> 
> Even using your code, I get the same output.
> 
> structure(list(X = c(-67.3804401, -67.36091, -67.3805899999999,
> -67.3397099999998, -67.3780199, -67.3221199999999), Y = c(-55.5655699999996,
> -55.5840098999999, -55.6004100000001, -55.6149699999997, -55.63521,
> -55.6400899999997), L1 = c(1, 1, 1, 1, 1, 1), L2 = c(1, 1, 1,
> 1, 1, 1), L3 = c(1, 1, 1, 1, 1, 1)), row.names = c(NA, 6L), class =
> "data.frame")
> 
> Thank you again.
> 
> On Mon, Nov 28, 2022 at 11:13 PM Barry Rowlingson <b.rowlingson using gmail.com>
> wrote:
> 
>> This seems to be a weird bug in `st_read`. If you read it with an SQL
>> query that matches every row it works:
>>
>>> js = st_read("./countrymasks.geojson", query="select * from countrymasks
>> where 1 = 1")
>> Reading query `select * from countrymasks where 1 = 1' from data
>> source `/home/rowlings/Downloads/countrymasks.geojson' using driver
>> `GeoJSON'
>> Simple feature collection with 214 features and 15 fields
>> Geometry type: MULTIPOLYGON
>> Dimension:     XY
>> Bounding box:  xmin: -180 ymin: -55.79439 xmax: 180 ymax: 83.62742
>> Geodetic CRS:  WGS 84
>>
>> But leave out the query and you get that C code level error. Another
>> equivalent query would be "select * from countrymasks" (without the
>> "where" clause) but this
>> triggers the error too. Very odd. Worth reporting as a bug?
>>
>> Barry
>>
>>
>>
>>
>> On Mon, Nov 28, 2022 at 8:08 PM Miluji Sb <milujisb using gmail.com> wrote:
>>>
>>> Thank you for reply. When I try using sf, I get the following error;
>>>
>>> Error in CPL_read_ogr(dsn, layer, query, as.character(options), quiet,  :
>>>    attempt to set index 210/210 in SET_STRING_ELT.
>>>
>>> Thanks again!
>>>
>>> On Mon, Nov 28, 2022 at 1:50 PM Josiah Parry <josiah.parry using gmail.com>
>> wrote:
>>>
>>>> You're going to want to read the file with sf.
>>>>
>>>> Try object <- sf::st_read("~countrymasks.geojson")
>>>>
>>>> On Mon, Nov 28, 2022 at 7:09 AM Miluji Sb <milujisb using gmail.com> wrote:
>>>>
>>>>> Greetings everyone,
>>>>>
>>>>> I would like to convert the geojson file (
>>>>>
>>>>>
>> https://drive.google.com/file/d/18h3sOjZg5jp5euLTWRi5mC40Sja8TZDN/view?usp=sharing
>>>>> )
>>>>> to a dataframe - essentially obtain which has coordinates matched to a
>>>>> country.
>>>>>
>>>>> I have tried the following;
>>>>>
>>>>> ###
>>>>> states <- geojsonsf::geojson_sf("~/countrymasks.geojson")
>>>>> geo <- geojsonsf::sf_geojson(states)
>>>>> sf <- sf::st_read(geo, quiet = T )
>>>>> df <- as.data.frame(sf::st_coordinates(sf) )
>>>>> ##
>>>>>
>>>>> But I get the following output, I am a bit lost. Any help will be
>> highly
>>>>> appreciated.
>>>>>
>>>>> Best,
>>>>>
>>>>>    structure(list(X = c(-67.3804401, -67.36091, -67.3805899999999,
>>>>> -67.3397099999998, -67.3780199, -67.3221199999999), Y =
>>>>> c(-55.5655699999996,
>>>>> -55.5840098999999, -55.6004100000001, -55.6149699999997, -55.63521,
>>>>> -55.6400899999997), L1 = c(1, 1, 1, 1, 1, 1), L2 = c(1, 1, 1,
>>>>> 1, 1, 1), L3 = c(1, 1, 1, 1, 1, 1)), row.names = c(NA, 6L), class =
>>>>> "data.frame")
>>>>>
>>>>>          [[alternative HTML version deleted]]
>>>>>
>>>>> _______________________________________________
>>>>> R-sig-Geo mailing list
>>>>> R-sig-Geo using r-project.org
>>>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>>>
>>>>
>>>
>>>          [[alternative HTML version deleted]]
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo using r-project.org
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
> 
> 	[[alternative HTML version deleted]]
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

-- 
Edzer Pebesma
Institute for Geoinformatics
Heisenbergstrasse 2, 48151 Muenster, Germany
Phone: +49 251 8333081



More information about the R-sig-Geo mailing list