簡體   English   中英

讀取存儲為 R 中創建的 CSV 文件的幾何圖形

[英]Reading geometries storaged as a CSV file created in R

我收到了一個在 R 中創建的 CSV 文件,其中包含哥倫比亞特區及其幾何形狀的一些信息。 但是,這種格式對我來說很奇怪,我無法將其轉換為 shapefile。

幾何圖形如下所示:

list(c(-77.002011, -76.999051, -76.995986, -76.994548, -76.989257, -76.989521, -76.98902, -76.988895, -76.99705, -77.000043, -77.000329, -77.002011, 38.951051, 38.951842, 38.951996, 38.952295, 38.952409, 38.948629, 38.946757, 38.946269, 38.94627, 38.94627, 38.946972, 38.951051))

如何將其用作多邊形,然后將其轉換為形狀文件?

看起來列表包含一個數字向量,當轉換為兩列矩陣時,它將提供描述多邊形頂點的經度/緯度列。 因此你可以這樣做:

library(sf)
#> Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE

df <- st_sf(st_sfc(st_polygon(lapply(data, matrix, ncol = 2)), crs = "WGS84"))
st_geometry(df) <- "geometry"

結果:

df
#> Simple feature collection with 1 feature and 0 fields
#> Geometry type: POLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -77.00201 ymin: 38.94627 xmax: -76.9889 ymax: 38.95241
#> Geodetic CRS:  WGS 84
#>                         geometry
#> 1 POLYGON ((-77.00201 38.9510...

我們可以通過在 DC 區域的黑白 map 上繪制紅色多邊形來確認這是正確的,我們看到這個多邊形與現有道路創建的邊界很好地重合:

library(ggmap)

m <- get_stamenmap(c(bottom = 38.94, top = 38.96, 
                     left = -77.01, right = -76.98), 
                   zoom = 15, maptype = "toner-lite")

ggmap(m) + geom_sf(data = df, inherit.aes = FALSE, fill = "red", alpha = 0.2)

在此處輸入圖像描述

創建於 2023-01-25,使用reprex v2.0.2


使用的數據

data <- list(c(-77.002011, -76.999051, -76.995986, -76.994548, -76.989257,
               -76.989521, -76.98902, -76.988895, -76.99705, -77.000043,
               -77.000329, -77.002011, 38.951051, 38.951842, 38.951996, 38.952295,
               38.952409, 38.948629, 38.946757, 38.946269, 38.94627, 38.94627,
               38.946972, 38.951051))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM