layik / geocoder

R package to find international geocodes
https://layik.github.io/geocoder/
Other
3 stars 0 forks source link

mongolite ndjson #2

Open dcooley opened 4 years ago

dcooley commented 4 years ago

You may be interested in my fork of mongolite, where I've added an ndjson argument to $find and $aggregate . This makes querying much easier for spatial objects

geo <- mongo$find( query = qry, ndjson = TRUE )
sf <- geojsonsf::geojson_sf( geo )
layik commented 4 years ago

Thanks @dcooley that will definitely be part of a package like this when we come to the querying stage.

Any suggestions how to use the fork? Steal bits of the code and credit you with it? Will it be moving to the main package?

dcooley commented 4 years ago

You may be interested in the discussion we had on the main mongolite page.

If you intend on releasing geocoder to CRAN then I don't think you can use my fork as it will be a github-only package. So you may have to use some of the other methods described in the discussion.

layik commented 4 years ago

Time to run your fork but will save participating in the discussion for later. Will air my views if any.

layik commented 4 years ago

@dcooley I would be grateful if you could guide me through it a little. Here is my current task which, I assume, was what you were trying to do with the fork.

Part 1: sf => MongoDB GeoJSON index Part 2: MongoDB GeoJSON index => sf.

I am working on Part 2: mongolite::find gets me a messy list of coordinates. How should I reassemble it into something geojsonsf or sf itself can read?

My navigation through your fork has not been so successful as you might gather.

Really appreciate your input and will credit it accordingly.

layik commented 4 years ago

Right, this is how I solved Part 2, for now:

con = mongolite::mongo("collection")
r = con$iterate(limit = 1)
r.sf = geojsonsf::geojson_sf(r$json())
class(r.sf)
#> [1] "sf"         "data.frame"

I will of course keep an eye to see where I would need ndjson and the fork.

SymbolixAU commented 4 years ago

This is my work flow for both your part1 and part2

# PART 1
## get any sf object
sf <- sf::st_read( system.file("./shape/nc.shp"), package = "sf")
## atomise it
geo <- geojsonsf::sf_geojson( sf, atomise = TRUE )
## insert 
con$insert( geo )
## add index
con$index(add = '{"geometry" : "2dsphere"}')

# PART 2
## query the data
geo <- con$find( query = '{}', ndjson = TRUE )
## the 'ndjson' argument returns a vector of geojson

## convert to sf
sf <- geojsonsf::geojson_sf( geo )

Hopefully this gives you an idea of how it works. But having said that, you won't be able to use the ndjson argument in any pacakge, so your solution is probably better for your needs (would be interesting to see any benchmarks comparing the two methods).

layik commented 4 years ago

Great, thank you.

layik commented 4 years ago
sf <- sf::st_read( system.file("./shape/nc.shp", package = "sf"))
#> Reading layer `nc' from data source `/Library/Frameworks/R.framework/Versions/3.5/Resources/library/sf/shape/nc.shp' using driver `ESRI Shapefile'
#> Simple feature collection with 100 features and 14 fields
#> geometry type:  MULTIPOLYGON
#> dimension:      XY
#> bbox:           xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> epsg (SRID):    4267
#> proj4string:    +proj=longlat +datum=NAD27 +no_defs
geo <- geojsonsf::sf_geojson( sf, atomise = TRUE )
con = mongolite::mongo(collection = "nc")
con$drop()
con$insert( geo )
#> List of 6
#>  $ nInserted  : int 100
#>  $ nMatched   : int 0
#>  $ nModified  : int 0
#>  $ nRemoved   : int 0
#>  $ nUpserted  : int 0
#>  $ writeErrors: list()
con$index(add = '{"geometry" : "2dsphere"}')
#>   v key._id key.geometry              name      ns 2dsphereIndexVersion
#> 1 2       1         <NA>              _id_ test.nc                   NA
#> 2 2      NA     2dsphere geometry_2dsphere test.nc                    3
geo <- con$iterate( query = '{}')
sf2 <- geojsonsf::geojson_sf( geo$json() )
nrow(sf) == nrow(sf2)
#> [1] TRUE

Created on 2020-02-17 by the reprex package (v0.3.0)

Hoping there is still space for a package called geocoder :)

layik commented 4 years ago

Please feel free to close the ticket but always welcome any other input.