Coder Social home page Coder Social logo

nngeo's Introduction

CRAN_Status_Badge CRAN_Downloads_Badge

nngeo

The goal of nngeo is to provide k-nearest neighbor join capabilities for spatial analysis in R.

Installation

CRAN version -

install.packages("nngeo")

GitHub version -

install.packages("devtools")
devtools::install_github("michaeldorman/nngeo")

Usage

Once installed, the library can be loaded as follows.

library(nngeo)
#> Loading required package: sf
#> Linking to GEOS 3.5.1, GDAL 2.2.2, proj.4 4.9.2

Example

The following code section finds the 10-nearest towns for each city using function st_nn.

data(towns)
data(cities)
nn = st_nn(cities, towns, k = 20, progress = FALSE)
nn
#> [[1]]
#>  [1] 29 40 87  2 49 43  9 11 63 36 37 52 10 44 46 70 48 65 39 35
#> 
#> [[2]]
#>  [1] 18 19 39 32 28 44 25 10 64 46 33 92 81 26 45 50 36 70 71 35
#> 
#> [[3]]
#>  [1] 69 80 61 68 60 58 30 57 89 56 22  7 24 78 13 84  6  3 59 20

By default, the result is a sparse list with the neighbor IDs.

The st_nn function can also be used as a geometry predicate function when performing spatial join with sf::st_join.

nearest_towns = st_join(cities, towns, join = st_nn, k = 3, progress = FALSE)
head(nearest_towns)
#> Simple feature collection with 6 features and 2 fields
#> geometry type:  POINT
#> dimension:      XY
#> bbox:           xmin: 34.78177 ymin: 31.76832 xmax: 35.21371 ymax: 32.0853
#> epsg (SRID):    4326
#> proj4string:    +proj=longlat +datum=WGS84 +no_defs
#>        name.x    name.y                  geometry
#> 1   Jerusalem AMMINADAV POINT (35.21371 31.76832)
#> 1.1 Jerusalem     ALMON POINT (35.21371 31.76832)
#> 1.2 Jerusalem ABU GHOSH POINT (35.21371 31.76832)
#> 2    Tel-Aviv      AZOR  POINT (34.78177 32.0853)
#> 2.1  Tel-Aviv    ADANIM  POINT (34.78177 32.0853)
#> 2.2  Tel-Aviv  AHI'EZER  POINT (34.78177 32.0853)

A helper function st_connect can be used to draw lines according to joined feature IDs. This may be useful for visualization or for network analysis.

l = st_connect(cities, towns, ids = nn, progress = FALSE)
plot(st_geometry(towns), col = "darkgrey")
plot(st_geometry(cities), col = "red", add = TRUE)
plot(st_geometry(l), add = TRUE)

nngeo's People

Contributors

michaeldorman avatar rushgeo avatar

Watchers

 avatar  avatar

nngeo's Issues

OpenMP checks

Before merging:

  • Check loop schedules
  • Make OpenMP optional (off by default for now?)
  • Test on Windows
  • Test with R 3.5.x

Later:

  • Profile the whole code for Vincenty path. Getting lower than expected speedup ratio.
  • Make thread count an optional parameter to function, or use R option like mc.cores
  • Consider making default thread count equal to number of physical cores (after more benchmarking)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.