从shapefile向postgis导入数据时的字符集编码问题
2009-09-29 01:11
393 查看
无论是用shp2pgsql还是GeoRuby自带的shp2sql.rb小工具都是完成这个导入任务的。但是过程中可能会遇到一个问题,就是字符集的编码问题。如果shapefile的属性字段中包含了非ASCII字符,比如我手里的这个包含德语道路名的数据集,而postgis的数据库字符集又是UTF-8,那么如果还用默认命令行参数导入的话,就会出现类似
PGError: ERROR: invalid byte sequence for encoding "UTF8": 0xdf73 (ActiveRecord::StatementInvalid)
HINT: This error can also happen if the byte sequence does not match the encoding expected by the server, which is controlled by "client_encoding".
这样的错误。这个错误是shp2sql.rb报出来的,如果用shp2pgsql,那么出来的错误信息会是:
utf8: Invalidor incomplete multibyte or wide character
其实错误的原因都是作为数据源的shapefile中dbf文件的字符编码和目标数据库-postgis所要求的(我这里是UTF-8)不匹配,导致导入程序按照默认的编码格式无法解析源文件中的某些字符造成的。
解决这个问题其实就只需告诉导入程序源文件到底使用的是什么编码字符集就可以了。对于shp2pgsql来说,用一条命令的话就是
shp2pgsql shapefilename_without_extension tablename dbname -W LATIN1 | psql -d dbname
其中-W参数就是用来指定字符集的,对于我的数据来说,德语字符集的代码是LATIN1。而对于shp2sql.rb,大同小异——在db.yml文件中把encoding参数给进去,写成encoding: ISO-8859-1,然后出来运行
ruby shp2sql.rb shapefilename_with_shp_extension
就可以了。
本文基于Ubuntu 9.04, Postgresql 8.3.8, PostGIS 1.3.3, Ruby 1.8, GeoRuby 1.3.4
参考文献:
[1]. PostgreSQL 8.3.8 Character Set Support
[2]. ASCII -> UTF-8 convert problems for importing (GIS) data (the most helpful one)
[3]. shp2pgsql and encoding issues
[4]. Shp2pgsql : unable to convert french UTF8 encoded shapefiles
[5]. solving character encoding problems for dbf files (interesting, but not so helpful to the problem this post mentioned)
PGError: ERROR: invalid byte sequence for encoding "UTF8": 0xdf73 (ActiveRecord::StatementInvalid)
HINT: This error can also happen if the byte sequence does not match the encoding expected by the server, which is controlled by "client_encoding".
这样的错误。这个错误是shp2sql.rb报出来的,如果用shp2pgsql,那么出来的错误信息会是:
utf8: Invalidor incomplete multibyte or wide character
其实错误的原因都是作为数据源的shapefile中dbf文件的字符编码和目标数据库-postgis所要求的(我这里是UTF-8)不匹配,导致导入程序按照默认的编码格式无法解析源文件中的某些字符造成的。
解决这个问题其实就只需告诉导入程序源文件到底使用的是什么编码字符集就可以了。对于shp2pgsql来说,用一条命令的话就是
shp2pgsql shapefilename_without_extension tablename dbname -W LATIN1 | psql -d dbname
其中-W参数就是用来指定字符集的,对于我的数据来说,德语字符集的代码是LATIN1。而对于shp2sql.rb,大同小异——在db.yml文件中把encoding参数给进去,写成encoding: ISO-8859-1,然后出来运行
ruby shp2sql.rb shapefilename_with_shp_extension
就可以了。
本文基于Ubuntu 9.04, Postgresql 8.3.8, PostGIS 1.3.3, Ruby 1.8, GeoRuby 1.3.4
参考文献:
[1]. PostgreSQL 8.3.8 Character Set Support
[2]. ASCII -> UTF-8 convert problems for importing (GIS) data (the most helpful one)
[3]. shp2pgsql and encoding issues
[4]. Shp2pgsql : unable to convert french UTF8 encoded shapefiles
[5]. solving character encoding problems for dbf files (interesting, but not so helpful to the problem this post mentioned)
相关文章推荐
- mysql导入导出数据中文乱码解决方法小结(1、navicat导入问题已解决,创建连接后修改连接属性,选择高级->将使用Mysql字符集复选框去掉,下拉框选择GBK->导入sql文件OK;2、phpmyadmin显示乱码的问题也解决,两步:1.将sql文件以utf8的字符集编码另存,2.将文件中sql语句中的字段字符集编码改成utf8,导入OK)
- oracle 10g 字符集不同的库之间的数据导入导出问题
- 数据库表字段字符集编码问题
- 字符集问题(Linux、oracle、终端等,导入导出数据)
- Mysql导入数据:编码问题和权限问题(Linux)
- PostGIS 导入shapefile时的编码问题
- MsSQL数据导入到Mongo的默认编码问题(正确导入Mongo的方法)
- MsSQL数据导入到Mongo的默认编码问题(正确导入Mongo的方法)
- SQL SERVER 从Excel导入数据时不同语言字符集的比较问题
- 导入空间数据到Postgis数据库的问题
- db2数据导入编码问题
- 编码素养问题收集4:MySQL数据导入导出如何证明结果是正确的?
- postgresq+postgis导入导出shape文件的编码问题
- Sybase BCP导入数据时碰到中文编码问题的解决方法
- Oracle数据导入时,字符集问题解决
- 将数据导入至mysql中应该注意的编码方式问题
- oracle导入数据和编码问题
- Oracle11g中数据的倒库和入库操作以及高版本数据导入低版本数据可能引发的问题
- BCP 数据导入问题 Unix系统中的文本文件换行符引发的问题