r-spatial / sf

Simple Features for R
https://r-spatial.github.io/sf/
Other
1.34k stars 298 forks source link

st_distance involving EMPTY point should return NA? #1055

Closed floswald closed 5 years ago

floswald commented 5 years ago

I have noted the following behaviour:

> empty <- st_as_sfc("POINT(EMPTY)")
> non<- st_as_sfc("POINT(2 1)")
> non
Geometry set for 1 feature 
geometry type:  POINT
dimension:      XY
bbox:           xmin: 2 ymin: 1 xmax: 2 ymax: 1
epsg (SRID):    NA
proj4string:    NA
POINT (2 1)
> st_distance(empty,non)
     [,1]
[1,]    0

ie the distance between an EMPTY point and a regular one is zero. For me this is an issue because I have a computation that delivers

# A tibble: 707 x 2
      CZ                 out
   <dbl>         <POINT [m]>
 1    60               EMPTY
 2   254               EMPTY
 3    20   (1032108 -570089)
 4    21               EMPTY
 5   303 (1004300 -374707.2)
 6   409 (1026132 -421744.8)
 7   558               EMPTY
 8   312               EMPTY
 9   190               EMPTY
10   188               EMPTY
# … with 697 more rows

i know where the EMPTY comes from, but I don't know how to change it into something that is NA-compliant. for example this doesnt work

st_as_sfc("POINT(NA NA)")
OGR: Corrupt data
Error in CPL_sfc_from_wkt(x) : OGR error

so, for my use case it would definitely be the best solution if

> st_distance(empty,non)
NA
edzer commented 5 years ago

Thanks! Should work like that, now.

floswald commented 5 years ago

wow, that was fast! thanks! say, just a quick question. I built sf from source on my mac (OS10.14) with an up to date homebrew gdal installation. but I get tons of warnings like

pj_obj_create: Cannot find proj.db
pj_obj_create: Cannot find proj.db

any idea what that could be? thanks!

edzer commented 5 years ago

Seems you have a broken PROJ installation: proj.db is part of PROJ 6.x.

floswald commented 5 years ago

I can build from source and sf loads correclty. also, simple commands with proj work on my commandline. I get the pj_obj_create: Cannot find proj.db error whenever I build the package from source, not when I use the CRAN version.

> devtools::install_github("r-spatial/sf",force=TRUE)
Using GitHub PAT from envvar GITHUB_PAT
Downloading GitHub repo r-spatial/sf@master
from URL https://api.github.com/repos/r-spatial/sf/zipball/master
Installing sf
'/Library/Frameworks/R.framework/Resources/bin/R' --no-site-file --no-environ  \
  --no-save --no-restore --quiet CMD INSTALL  \
  '/private/var/folders/ry/d__dq69j6w31gmbnhz7ybj013sn99p/T/Rtmp4dSvRZ/devtoolsef2e1f2951df/r-spatial-sf-7ac7e7e'  \
  --library='/Library/Frameworks/R.framework/Versions/3.5/Resources/library'  \
  --install-tests 

* installing *source* package ‘sf’ ...
configure: CC: gcc-9
configure: CXX: clang++ -std=gnu++11
checking for gdal-config... /usr/local/bin/gdal-config
checking gdal-config usability... yes
configure: GDAL: 2.4.1
checking GDAL version >= 2.0.1... yes
checking for gcc... gcc-9
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc-9 accepts -g... yes
checking for gcc-9 option to accept ISO C89... none needed
checking how to run the C preprocessor... gcc-9 -E
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for ANSI C header files... rm: conftest.dSYM: is a directory
rm: conftest.dSYM: is a directory
yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking gdal.h usability... yes
checking gdal.h presence... yes
checking for gdal.h... yes
checking GDAL: linking with --libs only... yes
checking GDAL: /usr/local/Cellar/gdal/2.4.1_1/share/gdal/pcs.csv readable... yes
checking GDAL: checking whether PROJ is available for linking:... yes
checking GDAL: checking whether PROJ is available fur running:... yes
configure: pkg-config proj exists, will use it
checking proj.h usability... yes
checking proj.h presence... yes
checking for proj.h... yes
configure: PROJ: 6.1.0
checking for proj_context_create in -lproj... yes
checking for geos-config... /usr/local/bin/geos-config
checking geos-config usability... yes
configure: GEOS: 3.7.2
checking GEOS version >= 3.4.0... yes
checking geos_c.h usability... yes
checking geos_c.h presence... yes
checking for geos_c.h... yes
checking geos: linking with -L/usr/local/Cellar/geos/3.7.2/lib -lgeos_c... yes
configure: Package CPP flags:  -I/usr/local/Cellar/proj/6.1.0/include -DHAVE_PROJ_H -I/usr/local/Cellar/gdal/2.4.1_1/include -I/usr/local/Cellar/geos/3.7.2/include
configure: Package LIBS: -L/usr/local/Cellar/proj/6.1.0/lib -lproj   -L/usr/local/Cellar/gdal/2.4.1_1/lib -lgdal -L/usr/local/Cellar/geos/3.7.2/lib -lgeos_c
configure: creating ./config.status
config.status: creating src/Makevars
** libs
clang++ -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/Cellar/proj/6.1.0/include -DHAVE_PROJ_H -I/usr/local/Cellar/gdal/2.4.1_1/include -I/usr/local/Cellar/geos/3.7.2/include -I"/Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include" -I/usr/local/include   -fPIC  -Wall -g -O2 -c RcppExports.cpp -o RcppExports.o
RcppExports.cpp:948:1: warning: unused variable 'stop_sym' [-Wunused-variable]
BEGIN_RCPP
^
/Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include/Rcpp/macros/macros.h:36:17: note: expanded from macro
      'BEGIN_RCPP'
    static SEXP stop_sym = Rf_install("stop");                                                   \
                ^
RcppExports.cpp:948:1: warning: unused variable 'nprot' [-Wunused-variable]
/Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include/Rcpp/macros/macros.h:32:9: note: expanded from macro
      'BEGIN_RCPP'
    int nprot = 0;                                                                               \
        ^
RcppExports.cpp:984:1: warning: unused variable 'nprot' [-Wunused-variable]
BEGIN_RCPP
^
/Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include/Rcpp/macros/macros.h:32:9: note: expanded from macro
      'BEGIN_RCPP'
    int nprot = 0;                                                                               \
        ^
RcppExports.cpp:984:1: warning: unused variable 'stop_sym' [-Wunused-variable]
/Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include/Rcpp/macros/macros.h:36:17: note: expanded from macro
      'BEGIN_RCPP'
    static SEXP stop_sym = Rf_install("stop");                                                   \
                ^
4 warnings generated.
clang++ -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/Cellar/proj/6.1.0/include -DHAVE_PROJ_H -I/usr/local/Cellar/gdal/2.4.1_1/include -I/usr/local/Cellar/geos/3.7.2/include -I"/Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include" -I/usr/local/include   -fPIC  -Wall -g -O2 -c bbox.cpp -o bbox.o
clang++ -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/Cellar/proj/6.1.0/include -DHAVE_PROJ_H -I/usr/local/Cellar/gdal/2.4.1_1/include -I/usr/local/Cellar/geos/3.7.2/include -I"/Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include" -I/usr/local/include   -fPIC  -Wall -g -O2 -c gdal.cpp -o gdal.o
clang++ -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/Cellar/proj/6.1.0/include -DHAVE_PROJ_H -I/usr/local/Cellar/gdal/2.4.1_1/include -I/usr/local/Cellar/geos/3.7.2/include -I"/Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include" -I/usr/local/include   -fPIC  -Wall -g -O2 -c gdal_geom.cpp -o gdal_geom.o
clang++ -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/Cellar/proj/6.1.0/include -DHAVE_PROJ_H -I/usr/local/Cellar/gdal/2.4.1_1/include -I/usr/local/Cellar/geos/3.7.2/include -I"/Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include" -I/usr/local/include   -fPIC  -Wall -g -O2 -c gdal_read.cpp -o gdal_read.o
clang++ -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/Cellar/proj/6.1.0/include -DHAVE_PROJ_H -I/usr/local/Cellar/gdal/2.4.1_1/include -I/usr/local/Cellar/geos/3.7.2/include -I"/Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include" -I/usr/local/include   -fPIC  -Wall -g -O2 -c gdal_utils.cpp -o gdal_utils.o
clang++ -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/Cellar/proj/6.1.0/include -DHAVE_PROJ_H -I/usr/local/Cellar/gdal/2.4.1_1/include -I/usr/local/Cellar/geos/3.7.2/include -I"/Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include" -I/usr/local/include   -fPIC  -Wall -g -O2 -c gdal_write.cpp -o gdal_write.o
clang++ -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/Cellar/proj/6.1.0/include -DHAVE_PROJ_H -I/usr/local/Cellar/gdal/2.4.1_1/include -I/usr/local/Cellar/geos/3.7.2/include -I"/Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include" -I/usr/local/include   -fPIC  -Wall -g -O2 -c geos.cpp -o geos.o
clang++ -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/Cellar/proj/6.1.0/include -DHAVE_PROJ_H -I/usr/local/Cellar/gdal/2.4.1_1/include -I/usr/local/Cellar/geos/3.7.2/include -I"/Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include" -I/usr/local/include   -fPIC  -Wall -g -O2 -c hex.cpp -o hex.o
clang++ -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/Cellar/proj/6.1.0/include -DHAVE_PROJ_H -I/usr/local/Cellar/gdal/2.4.1_1/include -I/usr/local/Cellar/geos/3.7.2/include -I"/Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include" -I/usr/local/include   -fPIC  -Wall -g -O2 -c ops.cpp -o ops.o
clang++ -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/Cellar/proj/6.1.0/include -DHAVE_PROJ_H -I/usr/local/Cellar/gdal/2.4.1_1/include -I/usr/local/Cellar/geos/3.7.2/include -I"/Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include" -I/usr/local/include   -fPIC  -Wall -g -O2 -c polygonize.cpp -o polygonize.o
clang++ -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/Cellar/proj/6.1.0/include -DHAVE_PROJ_H -I/usr/local/Cellar/gdal/2.4.1_1/include -I/usr/local/Cellar/geos/3.7.2/include -I"/Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include" -I/usr/local/include   -fPIC  -Wall -g -O2 -c proj.cpp -o proj.o
clang++ -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/Cellar/proj/6.1.0/include -DHAVE_PROJ_H -I/usr/local/Cellar/gdal/2.4.1_1/include -I/usr/local/Cellar/geos/3.7.2/include -I"/Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include" -I/usr/local/include   -fPIC  -Wall -g -O2 -c proj_info.cpp -o proj_info.o
clang++ -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/Cellar/proj/6.1.0/include -DHAVE_PROJ_H -I/usr/local/Cellar/gdal/2.4.1_1/include -I/usr/local/Cellar/geos/3.7.2/include -I"/Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include" -I/usr/local/include   -fPIC  -Wall -g -O2 -c raster2sf.cpp -o raster2sf.o
clang++ -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/Cellar/proj/6.1.0/include -DHAVE_PROJ_H -I/usr/local/Cellar/gdal/2.4.1_1/include -I/usr/local/Cellar/geos/3.7.2/include -I"/Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include" -I/usr/local/include   -fPIC  -Wall -g -O2 -c sfg.cpp -o sfg.o
clang++ -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/Cellar/proj/6.1.0/include -DHAVE_PROJ_H -I/usr/local/Cellar/gdal/2.4.1_1/include -I/usr/local/Cellar/geos/3.7.2/include -I"/Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include" -I/usr/local/include   -fPIC  -Wall -g -O2 -c signed_area.cpp -o signed_area.o
clang++ -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/Cellar/proj/6.1.0/include -DHAVE_PROJ_H -I/usr/local/Cellar/gdal/2.4.1_1/include -I/usr/local/Cellar/geos/3.7.2/include -I"/Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include" -I/usr/local/include   -fPIC  -Wall -g -O2 -c stars.cpp -o stars.o
clang++ -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/Cellar/proj/6.1.0/include -DHAVE_PROJ_H -I/usr/local/Cellar/gdal/2.4.1_1/include -I/usr/local/Cellar/geos/3.7.2/include -I"/Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include" -I/usr/local/include   -fPIC  -Wall -g -O2 -c wkb.cpp -o wkb.o
clang++ -std=gnu++11 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/usr/local/lib -o sf.so RcppExports.o bbox.o gdal.o gdal_geom.o gdal_read.o gdal_utils.o gdal_write.o geos.o hex.o ops.o polygonize.o proj.o proj_info.o raster2sf.o sfg.o signed_area.o stars.o wkb.o -L/usr/local/Cellar/proj/6.1.0/lib -lproj -L/usr/local/Cellar/gdal/2.4.1_1/lib -lgdal -L/usr/local/Cellar/geos/3.7.2/lib -lgeos_c -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
installing to /Library/Frameworks/R.framework/Versions/3.5/Resources/library/sf/libs
** R
** demo
** inst
** tests
** byte-compile and prepare package for lazy loading
in method for ‘dbWriteTable’ with signature ‘"PostgreSQLConnection","character","sf"’: no definition for class “PostgreSQLConnection”
in method for ‘dbDataType’ with signature ‘"PostgreSQLConnection","sf"’: no definition for class “PostgreSQLConnection”
in method for ‘coerce’ with signature ‘"Spatial","sf"’: no definition for class “Spatial”
in method for ‘coerce’ with signature ‘"Spatial","sfc"’: no definition for class “Spatial”
in method for ‘coerce’ with signature ‘"sf","Spatial"’: no definition for class “Spatial”
in method for ‘coerce’ with signature ‘"sfc","Spatial"’: no definition for class “Spatial”
in method for ‘coerce’ with signature ‘"XY","Spatial"’: no definition for class “Spatial”
** help
*** installing help indices
*** copying figures
** building package indices
** installing vignettes
** testing if installed package can be loaded
* DONE (sf)

My package loads fine after that:

> library(USfert)
Loading required package: dplyr

Attaching package: ‘dplyr’

The following objects are masked from ‘package:stats’:

    filter, lag

The following objects are masked from ‘package:base’:

    intersect, setdiff, setequal, union

Loading required package: sf
Linking to GEOS 3.7.2, GDAL 2.4.1, PROJ 6.1.0
Loading required package: ggplot2
Warning messages:
1: package ‘dplyr’ was built under R version 3.5.2 
2: replacing previous import ‘data.table::last’ by ‘dplyr::last’ when loading ‘USfert’ 
3: replacing previous import ‘data.table::first’ by ‘dplyr::first’ when loading ‘USfert’ 
4: replacing previous import ‘data.table::between’ by ‘dplyr::between’ when loading ‘USfert’ 

but computations involving st_distance display pj_obj_create: Cannot find proj.db