R-类AsI不适用于sf软件包

问题描述 投票:0回答:1

我正在使用Spotfire中的.shp文件,当我保存它时,几何数据将与“ AsIs”类一起保存。使用sf软件包和功能st_as_sfc(以便执行其他功能),我得到以下错误消息,如下所示。我在下面的示例中重新创建了它。

我如何将Geometry转换为SF可以使用的东西,或更具体地说是什么类?

library(sf)
nc <- st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE)

class(nc$geometry) <- "AsIs"
st_as_sfc(nc$geometry)

Error in UseMethod("st_as_sfc") :
  no applicable method for 'st_as_sfc' applied to an object of class "AsIs"*
r spatial shapefile sf
1个回答
0
投票

好吧,您的实际数据与您问题中的示例不太相似。

几何图形以原始格式表示为众所周知的二进制文件

library(sf)

load("~/Downloads/sec_shp.RData")


sec_shp$Geometry[[1]]
# [1] 01 03 00 00 00 01 00 00 00 06 00 00 00 0f d5 78 9b 80 63 59 c0 bb 0f 16 3b dd 1e 40 40 79 1f cd 16 3c 63 59 c0 04 bf
# [40] e8 1a 19 1d 40 40 d0 8f b7 4d 3b 63 59 c0 b5 71 ce eb 13 1d 40 40 44 a3 aa 2a 49 64 59 c0 dc 03 be fe 9b 1c 40 40 42
# [79] d0 d2 eb 8e 64 59 c0 65 05 c1 d3 64 1e 40 40 0f d5 78 9b 80 63 59 c0 bb 0f 16 3b dd 1e 40 40

您可以使用]阅读>

sf:::st_as_sfc.raw( sec_shp$Geometry[[1]] )
# Geometry set for 1 feature 
# geometry type:  POLYGON
# dimension:      XY
# bbox:           xmin: -101.5712 ymin: 32.22351 xmax: -101.5505 ymax: 32.24113
# CRS:            NA
# POLYGON ((-101.5547 32.24113, -101.5505 32.2273...

因此,要一次性阅读所有内容,请将类设置为WKB,然后调用st_as_sfc()


attr(sec_shp$Geometry, "class") <- c("WKB")

sfc <- sf::st_as_sfc( sec_shp$Geometry )

sec_shp$Geometry <- sfc

sf <- sf::st_as_sf( sec_shp )

您已完成


我很想看看您的数据是什么样,所以这里是

library(mapdeck)

set_token( secret::get_secret( "mapbox" ) )

mapdeck() %>%
  add_polygon(
    data = sf
    , fill_colour = "OBJECTID"
  )

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.