【问题标题】:R & Postgis: ERROR: Geometry type (Polygon) does not match column type (MultiPolygon)R&Postgis:错误:几何类型(多边形)与列类型(MultiPolygon)不匹配
【发布时间】:2022-02-01 12:11:59
【问题描述】:

下面的代码使我能够将在传单地图上绘制的多边形添加到数据库中的表中。 但是我遇到了一些困难,因为我的表格的几何列称为“geom”,而使用传单地图创建的几何列称为“几何”。

我收到如下警告消息:

"Error in : Failed to initialise COPY: ERREUR: the column "geometry" does not exists"

ui <- fluidPage(leafletOutput("map"))
 
server <-  function(input, output, session){
  output$map <- renderLeaflet({
  leaflet() %>%  
  addTiles(group = "OSM") %>%
  addDrawToolbar(targetLayerId = NULL, targetGroup = NULL,
                 polygonOptions = drawPolygonOptions(showArea = TRUE, metric=TRUE, shapeOptions=drawShapeOptions(fillColor="aquamarine", fillOpacity=0.8, clickable = TRUE), repeatMode=FALSE),
                 polylineOptions = FALSE, 
                 circleOptions = FALSE, 
                 rectangleOptions = FALSE, 
                 markerOptions = FALSE,
                 circleMarkerOptions = FALSE,
                 singleFeature = FALSE,
                 editOptions = FALSE)})

  observeEvent(input$map_draw_new_feature, {
polygon_coordinates <- input$map_draw_new_feature$geometry$coordinates[[1]]
drawn_polygon <- Polygon(do.call(rbind,lapply(polygon_coordinates,function(x){c(x[[1]][1],x[[2]][1])})))

sps <- SpatialPolygons(list(Polygons(list(drawn_polygon),"drawn_polygon")))
created_polygon  <- st_as_sf(sps, CRS("+proj=longlat +datum=WGS84"))

st_write(created_polygon , pool, "mytable", append = TRUE)
})


}

shinyApp(ui, server)

如何在不更改数据库中的列的情况下解决这个问题?


编辑:

我试过这样丑陋的东西:

created_polygon$geom <- created_polygon$geometry  
    
created_polygon<- created_polygon%>% st_drop_geometry()

但我收到此警告消息

"Warning: Error in : COPY returned error: ERROR: Geometry type (Polygon) does not match column type (MultiPolygon)"

【问题讨论】:

    标签: r postgresql shiny postgis sf


    【解决方案1】:

    猜测created_polygonsf 对象,您可以使用st_geometry(nc) &lt;- "geom" 重命名geometry 列。

    请在下面找到一个代表。

    注意:您需要有sf 1.0.6 或更高版本(如果没有,请取消reprex 第一行的注释)

    Reprex

    • 导入数据并读取nc对象(即geometry列名为"geometry"
    #> library(devtools)
    #> install_github("r-spatial/sf")
    
    library(sf)
    
    nc <- st_read(system.file("shape/nc.shp", package="sf"))
    
    nc
    #> 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
    #> First 10 features:
    #>     AREA PERIMETER CNTY_ CNTY_ID        NAME  FIPS FIPSNO CRESS_ID BIR74 SID74
    #> 1  0.114     1.442  1825    1825        Ashe 37009  37009        5  1091     1
    #> 2  0.061     1.231  1827    1827   Alleghany 37005  37005        3   487     0
    #> 3  0.143     1.630  1828    1828       Surry 37171  37171       86  3188     5
    #> 4  0.070     2.968  1831    1831   Currituck 37053  37053       27   508     1
    #> 5  0.153     2.206  1832    1832 Northampton 37131  37131       66  1421     9
    #> 6  0.097     1.670  1833    1833    Hertford 37091  37091       46  1452     7
    #> 7  0.062     1.547  1834    1834      Camden 37029  37029       15   286     0
    #> 8  0.091     1.284  1835    1835       Gates 37073  37073       37   420     0
    #> 9  0.118     1.421  1836    1836      Warren 37185  37185       93   968     4
    #> 10 0.124     1.428  1837    1837      Stokes 37169  37169       85  1612     1
    #>    NWBIR74 BIR79 SID79 NWBIR79                       geometry
    #> 1       10  1364     0      19 MULTIPOLYGON (((-81.47276 3...
    #> 2       10   542     3      12 MULTIPOLYGON (((-81.23989 3...
    #> 3      208  3616     6     260 MULTIPOLYGON (((-80.45634 3...
    #> 4      123   830     2     145 MULTIPOLYGON (((-76.00897 3...
    #> 5     1066  1606     3    1197 MULTIPOLYGON (((-77.21767 3...
    #> 6      954  1838     5    1237 MULTIPOLYGON (((-76.74506 3...
    #> 7      115   350     2     139 MULTIPOLYGON (((-76.00897 3...
    #> 8      254   594     2     371 MULTIPOLYGON (((-76.56251 3...
    #> 9      748  1190     2     844 MULTIPOLYGON (((-78.30876 3...
    #> 10     160  2038     5     176 MULTIPOLYGON (((-80.02567 3...
    
    • 重命名geometry 列的代码
    st_geometry(nc) <- "geom" 
    
    • 输出(即"geometry" 已更改为"geom"
    nc
    #> 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
    #> First 10 features:
    #>     AREA PERIMETER CNTY_ CNTY_ID        NAME  FIPS FIPSNO CRESS_ID BIR74 SID74
    #> 1  0.114     1.442  1825    1825        Ashe 37009  37009        5  1091     1
    #> 2  0.061     1.231  1827    1827   Alleghany 37005  37005        3   487     0
    #> 3  0.143     1.630  1828    1828       Surry 37171  37171       86  3188     5
    #> 4  0.070     2.968  1831    1831   Currituck 37053  37053       27   508     1
    #> 5  0.153     2.206  1832    1832 Northampton 37131  37131       66  1421     9
    #> 6  0.097     1.670  1833    1833    Hertford 37091  37091       46  1452     7
    #> 7  0.062     1.547  1834    1834      Camden 37029  37029       15   286     0
    #> 8  0.091     1.284  1835    1835       Gates 37073  37073       37   420     0
    #> 9  0.118     1.421  1836    1836      Warren 37185  37185       93   968     4
    #> 10 0.124     1.428  1837    1837      Stokes 37169  37169       85  1612     1
    #>    NWBIR74 BIR79 SID79 NWBIR79                           geom
    #> 1       10  1364     0      19 MULTIPOLYGON (((-81.47276 3...
    #> 2       10   542     3      12 MULTIPOLYGON (((-81.23989 3...
    #> 3      208  3616     6     260 MULTIPOLYGON (((-80.45634 3...
    #> 4      123   830     2     145 MULTIPOLYGON (((-76.00897 3...
    #> 5     1066  1606     3    1197 MULTIPOLYGON (((-77.21767 3...
    #> 6      954  1838     5    1237 MULTIPOLYGON (((-76.74506 3...
    #> 7      115   350     2     139 MULTIPOLYGON (((-76.00897 3...
    #> 8      254   594     2     371 MULTIPOLYGON (((-76.56251 3...
    #> 9      748  1190     2     844 MULTIPOLYGON (((-78.30876 3...
    #> 10     160  2038     5     176 MULTIPOLYGON (((-80.02567 3...
    

    reprex package (v2.0.1) 于 2022-02-01 创建


    编辑

    作为您评论的后续,请在下面找到将几何类型从 MULTIPOLYGON 更改为 POLYGON 的方法

    library(sf)
    
    # Geometry type at the origin : MULTIPOLYGON
    unique(st_geometry_type(nc))
    #> [1] MULTIPOLYGON
    #> 18 Levels: GEOMETRY POINT LINESTRING POLYGON MULTIPOINT ... TRIANGLE
    
    • 将几何类型从MULTIPOLYGON 转换为POLYGON 的代码
    nc <- st_cast(nc, "POLYGON")
    #> Warning in st_cast.sf(nc, "POLYGON"): repeating attributes for all sub-
    #> geometries for which they may not be constant
    
    • 输出:几何现在的类型为POLYGON
    unique(st_geometry_type(nc))
    #> [1] POLYGON
    #> 18 Levels: GEOMETRY POINT LINESTRING POLYGON MULTIPOINT ... TRIANGLE
    
    • 上次检查(请参阅geom 列)
    nc
    #> Simple feature collection with 108 features and 14 fields
    #> Geometry type: POLYGON
    #> Dimension:     XY
    #> Bounding box:  xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
    #> Geodetic CRS:  NAD27
    #> First 10 features:
    #>      AREA PERIMETER CNTY_ CNTY_ID        NAME  FIPS FIPSNO CRESS_ID BIR74 SID74
    #> 1   0.114     1.442  1825    1825        Ashe 37009  37009        5  1091     1
    #> 2   0.061     1.231  1827    1827   Alleghany 37005  37005        3   487     0
    #> 3   0.143     1.630  1828    1828       Surry 37171  37171       86  3188     5
    #> 4   0.070     2.968  1831    1831   Currituck 37053  37053       27   508     1
    #> 4.1 0.070     2.968  1831    1831   Currituck 37053  37053       27   508     1
    #> 4.2 0.070     2.968  1831    1831   Currituck 37053  37053       27   508     1
    #> 5   0.153     2.206  1832    1832 Northampton 37131  37131       66  1421     9
    #> 6   0.097     1.670  1833    1833    Hertford 37091  37091       46  1452     7
    #> 7   0.062     1.547  1834    1834      Camden 37029  37029       15   286     0
    #> 8   0.091     1.284  1835    1835       Gates 37073  37073       37   420     0
    #>     NWBIR74 BIR79 SID79 NWBIR79                           geom
    #> 1        10  1364     0      19 POLYGON ((-81.47276 36.2343...
    #> 2        10   542     3      12 POLYGON ((-81.23989 36.3653...
    #> 3       208  3616     6     260 POLYGON ((-80.45634 36.2425...
    #> 4       123   830     2     145 POLYGON ((-76.00897 36.3196...
    #> 4.1     123   830     2     145 POLYGON ((-76.02717 36.5567...
    #> 4.2     123   830     2     145 POLYGON ((-75.90199 36.5562...
    #> 5      1066  1606     3    1197 POLYGON ((-77.21767 36.2409...
    #> 6       954  1838     5    1237 POLYGON ((-76.74506 36.2339...
    #> 7       115   350     2     139 POLYGON ((-76.00897 36.3196...
    #> 8       254   594     2     371 POLYGON ((-76.56251 36.3405...
    

    reprex package (v2.0.1) 于 2022 年 2 月 1 日创建

    【讨论】:

    • 您的回答看起来简单而有希望,但似乎我无法下载软件包 'sf' 1.0-5 的更新版本;(
    • @zakros,感谢您的反馈。您能否多解释一下您在尝试安装sf 的开发版本时遇到的问题?您使用的是 Windows 还是 MacOS?
    • 我收到许多警告行以这条错误消息结尾:In i.p(...) : installation of package ‘C:/Users/USER~1/AppData/Local/Temp/Rtmpe8ru7f/file2a8074054ffd/sf_1.0-6.tar.gz’ had non-zero exit status。我正在使用 Windows,但 sf 的这个问题可能仍然是一个问题,因为我正在开发的闪亮应用程序必须部署在我闪亮的服务器上......
    • @zakros,感谢您的澄清。如果您在 Windows 上工作,reprex 中表示为 cmets 的前两行代码通常应该可以工作...当我尝试更新某些库时,有时也会收到与您相同的错误消息。在这种情况下,(i)我关闭 R 中所有打开的文件,(ii)我关闭 R,(iii)我再次打开 R,并且(4)在这种情况下,我运行:library(devtools)install_github("r-spatial/sf") ...它应该可以工作...我为你祈祷:-)
    • 是的,我明白了。您只需使用 sf 库中的 st_cast() 函数更改几何类型。这取决于您自己尝试或添加您的数据摘录(例如使用dput(head(created_polygon))),以便我可以帮助您。
    猜你喜欢
    • 2014-11-10
    • 1970-01-01
    • 2018-06-08
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多