Skip to content

Instantly share code, notes, and snippets.

@elipousson
Created October 13, 2023 02:19
Show Gist options
  • Save elipousson/9d43c77ab9cf6d463d0356cb538b7110 to your computer and use it in GitHub Desktop.
Save elipousson/9d43c77ab9cf6d463d0356cb538b7110 to your computer and use it in GitHub Desktop.
library(sf)
#> Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE

# You can read data from these URLs

# GeoJSON (geojson)
read_sf("https://github.com/ropensci/geojsonio/raw/main/inst/examples/california.geojson")
#> Simple feature collection with 1 feature and 11 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -124.4096 ymin: 32.53416 xmax: -114.1315 ymax: 42.00952
#> Geodetic CRS:  WGS 84
#> # A tibble: 1 × 12
#>   name       abbreviation capital    city   population   area waterarea landarea
#>   <chr>      <chr>        <chr>      <chr>       <int>  <int>     <int>    <int>
#> 1 California CA           Sacramento Los A…   38332521 423968     20502   403466
#> # ℹ 4 more variables: houseseats <int>, statehood <date>, group <chr>,
#> #   geometry <MULTIPOLYGON [°]>

# Keyhole Markup Language (kml)
read_sf("https://github.com/ropensci/geojsonio/raw/main/inst/examples/poa_annua.kml")
#> Simple feature collection with 161 features and 2 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: -167.15 ymin: 0 xmax: 0 ymax: 69.3333
#> Geodetic CRS:  WGS 84
#> # A tibble: 161 × 3
#>    Name                                    Description             geometry
#>    <chr>                                   <chr>                <POINT [°]>
#>  1 "Patterson's bluegrass, Patterson blue… "<table wi… (-113.9553 43.39448)
#>  2 "Patterson's bluegrass, Patterson blue… "<table wi…  (-114.1191 46.0793)
#>  3 "Patterson's bluegrass, Patterson blue… "<table wi…  (-114.1191 46.0793)
#>  4 "Patterson's bluegrass, Patterson blue… "<table wi…  (-114.1191 46.0793)
#>  5 "Patterson's bluegrass, Patterson blue… "<table wi… (-109.5936 44.49239)
#>  6 "Patterson's bluegrass, Patterson blue… "<table wi…  (-114.1191 46.0793)
#>  7 "Patterson's bluegrass, Patterson blue… "<table wi…   (-109.817 40.7667)
#>  8 "Patterson's bluegrass, Patterson blue… "<table wi…            (-105 39)
#>  9 "Patterson's bluegrass, Patterson blue… "<table wi… (-106.1376 39.62102)
#> 10 "Patterson's bluegrass, Patterson blue… "<table wi…   (-111.183 40.6167)
#> # ℹ 151 more rows

# GeoPackage (gpkg)
read_sf("https://github.com/r-spatial/sf/raw/main/inst/gpkg/nc.gpkg")
#> Simple feature collection with 100 features and 14 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> Geodetic CRS:  NAD27
#> # A tibble: 100 × 15
#>     AREA PERIMETER CNTY_ CNTY_ID NAME  FIPS  FIPSNO CRESS_ID BIR74 SID74 NWBIR74
#>    <dbl>     <dbl> <dbl>   <dbl> <chr> <chr>  <dbl>    <int> <dbl> <dbl>   <dbl>
#>  1 0.114      1.44  1825    1825 Ashe  37009  37009        5  1091     1      10
#>  2 0.061      1.23  1827    1827 Alle… 37005  37005        3   487     0      10
#>  3 0.143      1.63  1828    1828 Surry 37171  37171       86  3188     5     208
#>  4 0.07       2.97  1831    1831 Curr… 37053  37053       27   508     1     123
#>  5 0.153      2.21  1832    1832 Nort… 37131  37131       66  1421     9    1066
#>  6 0.097      1.67  1833    1833 Hert… 37091  37091       46  1452     7     954
#>  7 0.062      1.55  1834    1834 Camd… 37029  37029       15   286     0     115
#>  8 0.091      1.28  1835    1835 Gates 37073  37073       37   420     0     254
#>  9 0.118      1.42  1836    1836 Warr… 37185  37185       93   968     4     748
#> 10 0.124      1.43  1837    1837 Stok… 37169  37169       85  1612     1     160
#> # ℹ 90 more rows
#> # ℹ 4 more variables: BIR79 <dbl>, SID79 <dbl>, NWBIR79 <dbl>,
#> #   geom <MULTIPOLYGON [°]>

# Comma-separated values (csv)
read_sf("https://github.com/r-spatial/sf/raw/main/inst/csv/pt.csv")
#> Simple feature collection with 3 features and 4 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 0 ymin: 0 xmax: 44 ymax: 55
#> CRS:           NA
#> # A tibble: 3 × 5
#>   Id    Int64        Int32 WKT          geometry
#>   <chr> <chr>        <chr> <chr>         <POINT>
#> 1 1     "0"          "33"  POINT(0 1)      (0 1)
#> 2 2     "4294967297" "44"  POINT(44 55)  (44 55)
#> 3 3     ""           ""    POINT(0 0)      (0 0)

# You can't read data from these URLs

# Shapefile (shp)
read_sf("https://github.com/r-spatial/sf/raw/main/inst/shape/nc.shp")
#> Warning in CPL_read_ogr(dsn, layer, query, as.character(options), quiet, : GDAL
#> Error 4: Unable to open /tmp/nc.shx or /tmp/nc.SHX. Set SHAPE_RESTORE_SHX
#> config option to YES to restore or create it.
#> Error: Cannot open "https://github.com/r-spatial/sf/raw/main/inst/shape/nc.shp"; The file doesn't seem to exist.

# ArcGIS FeatureServer URL
read_sf("https://geodata.md.gov/imap/rest/services/BusinessEconomy/MD_IncentiveZones/FeatureServer/2")
#> Warning in CPL_read_ogr(dsn, layer, query, as.character(options), quiet, : GDAL
#> Error 4: Failed to read TopoJSON data
#> Error: Cannot open "https://geodata.md.gov/imap/rest/services/BusinessEconomy/MD_IncentiveZones/FeatureServer/2"; The file doesn't seem to exist.

Created on 2023-10-12 with reprex v2.0.2

library(sf)
# You can read data from these URLs
# GeoJSON (geojson)
read_sf("https://github.com/ropensci/geojsonio/raw/main/inst/examples/california.geojson")
# Keyhole Markup Language (kml)
read_sf("https://github.com/ropensci/geojsonio/raw/main/inst/examples/poa_annua.kml")
# GeoPackage (gpkg)
read_sf("https://github.com/r-spatial/sf/raw/main/inst/gpkg/nc.gpkg")
# Comma-separated values (csv)
read_sf("https://github.com/r-spatial/sf/raw/main/inst/csv/pt.csv")
# You can't read data from these URLs
# Shapefile (shp)
read_sf("https://github.com/r-spatial/sf/raw/main/inst/shape/nc.shp")
# ArcGIS FeatureServer URL
read_sf("https://geodata.md.gov/imap/rest/services/BusinessEconomy/MD_IncentiveZones/FeatureServer/2")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment