writeogr层创建失败-mysql

8xiog9wr  于 2021-06-23  发布在  Mysql
关注(0)|答案(2)|浏览(356)

我正在尝试将多多边形边界加载到mysql。我已经试过了,
安装了gdal和mysql驱动程序。核对 ogrDrivers() 在r中,它返回驱动程序列表中的mysql。

db <- 'testdb'
user <- 'testuser'
pwd <- '****'
host <- '127.0.0.1'

DSN = paste0('MySQL:',db ,',user=', user, ',host=', host, ',password=', pwd)

data_2013 <- readOGR(dsn = "/Path/to/shapefile", layer = "testinglayer")

writeOGR(data_2013, dsn=DSN, driver='MySQL',layer='testinglayer', layer_options='ENGINE=Aria', overwrite_layer = TRUE)

但我有个错误,

Error in writeOGR(data_2013, dsn = DSN, driver = "MySQL", layer = "testinglayer",  : 
  Layer creation failed

或者有没有其他方法可以将空间多边形数据从r加载到mysql?
编辑:根据要求,

> ogrDrivers()$name
 [1] "AeronavFAA"     "AmigoCloud"     "ARCGEN"         "AVCBin"         "AVCE00"        
 [6] "BNA"            "CAD"            "Carto"          "Cloudant"       "CouchDB"       
[11] "CSV"            "CSW"            "DGN"            "DXF"            "EDIGEO"        
[16] "ElasticSearch"  "ESRI Shapefile" "Geoconcept"     "GeoJSON"        "Geomedia"      
[21] "GeoRSS"         "GFT"            "GML"            "GMLAS"          "GPKG"          
[26] "GPSBabel"       "GPSTrackMaker"  "GPX"            "HTF"            "HTTP"          
[31] "Idrisi"         "Interlis 1"     "Interlis 2"     "JML"            "JP2OpenJPEG"   
[36] "JPEG2000"       "KML"            "LIBKML"         "MapInfo File"   "Memory"        
[41] "MSSQLSpatial"   "MySQL"          "NAS"            "netCDF"         "ODBC"          
[46] "ODS"            "OGR_DODS"       "OGR_GMT"        "OGR_PDS"        "OGR_SDTS"      
[51] "OGR_VRT"        "OpenAir"        "OpenFileGDB"    "OSM"            "PCIDSK"        
[56] "PGDUMP"         "PGeo"           "PLSCENES"       "REC"            "S57"           
[61] "SEGUKOOA"       "SEGY"           "Selafin"        "SQLite"         "SUA"           
[66] "SVG"            "SXF"            "TIGER"          "UK .NTF"        "VDV"           
[71] "VFK"            "Walk"           "WAsP"           "WFS"            "XLS"           
[76] "XLSX"           "XPlane"
wqlqzqxt

wqlqzqxt1#

查看此链接时,参数必须用逗号分隔。所以也许这会奏效:

DSN = paste0('MYSQL:dbname=',db ,', user=', user, ', host=', host, ', password=', pwd, ', port=', port)

或者你试过 sf 包裹?

DSN = paste0('MYSQL:dbname=',db ,' user=', user, ' host=', host, ' port=', port, ' password=', pwd)

library(sf)
data(meuse)
st_write(st_as_sf(meuse, coords = c("x","y")), dsn = DSN,  layer = "testinglayer")

或改变 host <- '127.0.0.1'host <- "localhost" ?

kadbb459

kadbb4592#

首先,感谢@sega的不断帮助,我终于可以加载数据了。但有一些修正,这就是为什么我要详细说明它作为一个单独的答案。

library(sf)
fname <- "/path/to/shapefile/data_2013.shp"
data_2013 <- st_read(fname)

db <- 'testdb'
user <- 'testuser'
pwd <- '****'
host <- '127.0.0.1'
port <- 3306
DSN = paste0('MySQL:',db ,',user=', user,',password=', pwd, ',port=', port)

st_write(obj = data_2013, dsn = DSN, layer = "data_2013")

dsn必须与上述内容完全相同,其中没有“dbname”字符串。而且,顺序必须和这个一模一样。因为,格达尔希望,如果你看看这个链接-http://www.gdal.org/drv_mysql.html
我试过用mysql连接,但没用,但给mysql dsn就行了。唯一的问题是,这会在数据库中创建一些额外的表(geometry\u columns、spatial\u ref\u sys)。
再次感谢您对世嘉的帮助。

相关问题