Reputation: 4082
I am building a function to generate metadata for shapefiles in R.
One of my ideas was to get the name and class of each column in an SF (in case there is a massive SF and you don't have to wait to read all the data to get the names of them), for that I was looking into the read_sf
function, specifically to at the query argument.
Following the example from this link, I saw that you can filter by the values of a column to get a smaller dataset for example:
library(sf)
nc = st_read(system.file("shape/nc.shp", package="sf"))
If I know the name of the columns beforehand I could filter with something like this:
nc_sql = st_read(system.file("shape/nc.shp", package="sf"),
query = "SELECT NAME, SID74, FIPS FROM \"nc\" WHERE BIR74 > 20000")
But of course the goal is to get the column names, so I would like to use something similar to what I see in this answer.
I have tried this code among other unsuccessful tries:
nc_sql = st_read(system.file("shape/nc.shp", package="sf"),
query = "SELECT TOP 1 * FROM \"nc\"")
Any help would be greatly apprecieated
Upvotes: 1
Views: 947
Reputation: 128
You can pass a SQL query with read_sf. It should look like this
library(sf)
data = read_sf('file.gpkg', query = 'SELECT * from layername LIMIT 1')
Upvotes: 1
Reputation: 3604
Use rgdal::ogrInfo
ogrInfo(system.file("shape/nc.shp", package="sf"))
and the result:
Source: "/home/sapi/R/x86_64-pc-linux-gnu-library/4.1/sf/shape/nc.shp", layer: "nc"
Driver: ESRI Shapefile; number of rows: 100
Feature type: wkbPolygon with 2 dimensions
Extent: (-84.32385 33.88199) - (-75.45698 36.58965)
CRS: +proj=longlat +datum=NAD27 +no_defs
LDID: 87
Number of fields: 14
name type length typeName
1 AREA 2 24 Real
2 PERIMETER 2 24 Real
3 CNTY_ 2 24 Real
4 CNTY_ID 2 24 Real
5 NAME 4 80 String
6 FIPS 4 80 String
7 FIPSNO 2 24 Real
8 CRESS_ID 0 9 Integer
9 BIR74 2 24 Real
10 SID74 2 24 Real
11 NWBIR74 2 24 Real
12 BIR79 2 24 Real
13 SID79 2 24 Real
14 NWBIR79 2 24 Real
Upvotes: 2