Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Field name with parentheses or spaces #65

Open
tohka opened this issue Apr 18, 2020 · 3 comments
Open

Field name with parentheses or spaces #65

tohka opened this issue Apr 18, 2020 · 3 comments

Comments

@tohka
Copy link

tohka commented Apr 18, 2020

Hi,

If the attribute name contains parentheses or spaces, R replaces them with dots.
However, Layer[[Field]] is null because the name of the field before the replacement is used.

the log is

Input parameters:
{ 'Field' : 'H (m)', 'Layer' : 'C:/Users/username/Documents/サンプル.gpkg|layername=サンプル' }

R execution commands
options("repos"="http://cran.at.r-project.org/")
.libPaths("C:/Users/username/AppData/Roaming/QGIS/QGIS3/profiles/default/processing/rlibs")
tryCatch(find.package("sf"), error = function(e) install.packages("sf", dependencies=TRUE))
library("sf")
tryCatch(find.package("raster"), error = function(e) install.packages("raster", dependencies=TRUE))
library("raster")
Layer <- st_read("C:/Users/username/Documents/サンプル.gpkg", layer = "サンプル", quiet = TRUE, stringsAsFactors = FALSE)
Field <- "H (m)"
print(names(Layer))

R execution console output
Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
要求されたパッケージ sp をロード中です
[1] "H" "H..m." "geometry"
警告メッセージ:
パッケージ 'raster' はバージョン 3.6.3 の R の下で造られました
Execution completed in 1.40 seconds
Results:
{}
@JanCaha
Copy link
Collaborator

JanCaha commented Apr 19, 2020

This could be rather simple to fix.

Please can you take a look which characters get replaced? If it is only () or also [], etc.

Any idea if it will happen if you try to load the data using sp package with ##load_vector_using_rgdal?

@tohka
Copy link
Author

tohka commented Apr 30, 2020

Presumably, all non-alphanumeric characters except for underscores will be replaced by dots. When the check.names option is FALSE, read.table and data.frame will also behave in the same way. If the substitution results in the same name as the other column name, digits are appended to the end, so the substitution alone does not tell if it is a correct column name or not.

# test1.R
d <- data.frame(`x `=1, `x(`=2, `x_`=3, `x=`=4, `x@`=5)
names(d)
$ rscript test1.R
[1] "x."   "x..1" "x_"   "x..2" "x..3"

# test2.R
d <- data.frame(`x `=1, `x(`=2, `x_`=3, `x=`=4, `x@`=5, check.names=FALSE)
names(d)
$ rscript test2.R
[1] "x " "x(" "x_" "x=" "x@"

# test3.R
library(rgdal)

ogrInfo("sample.gpkg", layer="sample")
$ rscript test3.R
Loading required package: sp
rgdal: version: 1.4-8, (SVN revision 845)
 Geospatial Data Abstraction Library extensions to R successfully loaded
 Loaded GDAL runtime: GDAL 2.2.3, released 2017/11/20
 Path to GDAL shared files: C:/Users/username/bin/R-Portable/App/R-Portable/librar GDAL binary built with GEOS: TRUE
 Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
 Path to PROJ.4 shared files: C:/Users/username/bin/R-Portable/App/R-Portable/libr Linking to sp version: 1.3-2
Source: "C:\Users\username\Desktop\GIS\sample.gpkg", layer: "sample"
Driver: GPKG; number of rows: 5
Feature type: wkbPoint with 2 dimensions
Extent: (-0.02076547 -0.2517086) - (0.474755 0.1368101)
CRS: +proj=longlat +datum=WGS84 +no_defs
Number of fields: 5
  name type length typeName
1   x     0      0  Integer
2   x(    0      0  Integer
3   x_    0      0  Integer
4   x=    0      0  Integer
5   x@    0      0  Integer

# test4.R
library(sf)

pt <- st_read("sample.gpkg", layer="sample")
names(pt)
$ rscript test4.R
Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
Reading layer `sample' from data source `C:\Users\username\Desktop\GIS\sample.gpkg' using driver `GPKG'
Simple feature collection with 5 features and 5 fields
geometry type:  POINT
dimension:      XY
bbox:           xmin: -0.02076547 ymin: -0.2517086 xmax: 0.474755 ymax: 0.1368101
epsg (SRID):    4326
proj4string:    +proj=longlat +datum=WGS84 +no_defs
[1] "x."       "x..1"     "x_"       "x..2"     "x..3"     "geometry"

# test5.R
library(sf)

pt <- st_read("sample.gpkg", layer="sample", as_tibble=TRUE)
names(pt)
$ rscript test5.R
Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
Reading layer `sample' from data source `C:\Users\username\Desktop\GIS\sample.gpkg' using driver `GPKG'
Simple feature collection with 5 features and 5 fields
geometry type:  POINT
dimension:      XY
bbox:           xmin: -0.02076547 ymin: -0.2517086 xmax: 0.474755 ymax: 0.1368101
epsg (SRID):    4326
proj4string:    +proj=longlat +datum=WGS84 +no_defs
[1] "x."       "x..1"     "x_"       "x..2"     "x..3"     "geometry"

# test6.R
library(rgdal)

pt <- readOGR("sample.gpkg", layer="sample")
names(pt)
$ rscript test6.R
Loading required package: sp
rgdal: version: 1.4-8, (SVN revision 845)
 Geospatial Data Abstraction Library extensions to R successfully loaded
 Loaded GDAL runtime: GDAL 2.2.3, released 2017/11/20
 Path to GDAL shared files: C:/Users/username/bin/R-Portable/App/R-Portable/library/rgdal/gdal
 GDAL binary built with GEOS: TRUE
 Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
 Path to PROJ.4 shared files: C:/Users/username/bin/R-Portable/App/R-Portable/library/rgdal/proj
 Linking to sp version: 1.3-2
OGR data source with driver: GPKG
Source: "C:\Users\username\Desktop\GIS\sample.gpkg", layer: "sample"
with 5 features
It has 5 fields
[1] "x."   "x..1" "x_"   "x..2" "x..3"

@gavg712
Copy link
Contributor

gavg712 commented May 3, 2022

In sf version 1.0-8, there will be an extra argument in st_read() to optionally pass name checking (sf:#1916). In that way the names that originally come from the user data will be preserved, even if they contain special characters.

It will be good to consider adding it in the future in the parser for vector parameter as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants