设为首页 收藏本站
查看: 1181|回复: 0

[经验分享] PostGIS数据载入与检索

[复制链接]

尚未签到

发表于 2016-11-22 04:46:13 | 显示全部楼层 |阅读模式
PostGIS数据载入与导出常见的方法有三种:
1.使用SQL语句  这是最简单最直观的加载数据的方法,只是稍稍不同于普通的insert语句
如数据库中的表结构如下
        CREATE TABLE "sites" (gid serial PRIMARY KEY,
                                          "id"         int4,
                                           "cover"   varchar(20));
        SELECT AddGeometryColumn('','sites','the_geom','4269','POINT',2);
--加载数据的脚本roads.sql

BEGIN;

INSERT INTO "sites" ("id","cover",the_geom) VALUES ('1','shrubs','
                          POINT(455552.418360864 4641822.05368488)');INSERT INTO "sites" ("id","cover",the_geom) VALUES ('2','trees','
                          POINT(441201.65343075 4619029.66232529)');INSERT INTO "sites" ("id","cover",the_geom) VALUES ('3','rocks','
                          POINT(483093.224587039 4642560.69599746)');  。。。

COMMIT;
可以通过psql执行脚本从而将数据导入到PostgreSQL中
#psql -d postgis -f roads.sql
  从数据库中检索数据,和普通的sql查询没有什么区别,如下面这个语句是检索cover字段值为‘grass’的所有要素


postgis=# select ogc_fid,ST_AsEWKT(wkb_geometry),cover from sites where cover='grass';  2.shp2pgsql和pgsql2shp、shp2pgsql-gui
(1)shp2pgsql命令行
用法: shp2pgsql [<选项>] <shp文件> [<schema>.]<表名>
shp2pgsql最简单的形式就是 shp2pgsql shapefile.shp形式,如下

[postgres@localhost ~]$ shp2pgsql sites.shp
Shapefile type: Point  <==要素类型
Postgis type: POINT[2]
----------------------------------------------------------------------
SET CLIENT_ENCODING TO UTF8;
SET STANDARD_CONFORMING_STRINGS TO ON;
BEGIN;
CREATE TABLE "sites" (gid serial PRIMARY KEY, -- 其实默认情况下表名和shp文件名相同
                                  "id" int4,
                                   "cover" varchar(20));
SELECT AddGeometryColumn('','sites','the_geom','-1','POINT',2);
INSERT INTO "sites" ("id","cover",the_geom) VALUES                                                ('1','shrubs','010100000054CA66AC01CE1B41B4926F8307B55141');
INSERT INTO "sites" ("id","cover",the_geom) VALUES ('2','trees','0101000000D521085D9F761B4105868CA032AD5141');
  INSERT INTO "sites" ("id","cover",the_geom) VALUES ('3','rocks','010100000051F31C9DC6ED1A419989636AC59E5141');
  。。。
  commit;

  上面的一长窜数字是geometry的wkb形式,到这里是不是就很明了啊,其实shp2pgsql的作用就是把shp文件转换为SQL语句,再将产生的定义表结构和插入数据sql语句用 ">" 重定向到指定文件中,如下面的例子将转换结果重定向到sites.sql
[postgres@localhost ~]$ shp2pgsql sites.shp sites > sites.sql
可以通过shp2pgsql -?来查看shp2pgsql的用法
常用的选项是-
  -(c|a|d|p) 互斥选项
        -c 默认选项,创建新表和插入数据
        -a 追加shape文件到已存在的表中
        -d 先删除表,再创建表和插入数据
        -p 只是创建表
    -g 指定几何要素的列名
    -I 创建空间索引
    -s 生成简单要素,而不是几何对象集合(multiGeometry)
[postgres@localhost ~]$shp2pgsql -c -s 4269 -I sites.shp sites > sites.sql
(2)pgsql2shp
用法:pgsql2shp [<选项>] <数据库> <表名>
pgsql2shp [<选项>] <数据库> <查询语句>
常用选项:
    -f 导出文件名
    -h 主机地址
    -p 端口号
    -P 密码
    -u 用户名
将数据库postgis中的sites表导出到sites.shp,指定用户名和密码

[postgres@localhost ~]$pgsql2shp -f sites2.shp -P 123456 -u postgres postgis sites
  将数据库postgis中的sites表按照sql查询语句导出到sites.shp,指定用户名和密码

[postgres@localhost ~]$pgsql2shp -f sites3.shp -P 123456 -u postgres postgis "select * from sites where cover='grass'"
  (3)shp2pgsql-gui 就不多说了,傻瓜型的图形界面的导入工具
3.利用ogr2ogr命令行工具
      shp2pgsql等工具虽然很好用,但是他们最大的缺点就在于其使用范围有限,只能够针对Shape文件格式。而实际应用过程中可能会碰到各种各样的数据格式,这个时候就得需要ogr2ogr上场了,它gdal的一个命令行工具,支持目前主流的数据格式,如ESRI Shapefile、MapInfo等数据格式,在命令行上直接敲ogr2ogr就会显示其帮助信息,帮助信息中包括它支持的所有格式的详细信息。
  下面就简单地介绍ogr2ogr常用的一些操作
ogr2ogr --help  <==查看帮助信息
数据转换
(1)MapInfo ==> ESRI Shape



[postgres@localhost ~]$ ogr2ogr -f "ESRI Shapefile" mydata.shp mydata.tab
  (2)ESRI Shape ==>MapInfo



[postgres@localhost ~]$ ogr2ogr -f "MapInfo File" tabsites.tab sites.shp
  (3)MapInfo ==> PostGIS


[postgres@localhost ~]$ ogr2ogr -f "PostgreSQL" PG:"host=localhost user=postgres dbname=postgis password=123456"
  (4)postgis==>ESRI Shapefile


[postgres@localhost ~]$ ogr2ogr -f "ESRI Shapefile" mydata.shp PG:"host=localhost dbname=postgis user=postgres password=123456" "mytable"
  (5)PostGIS ==> KML


[postgres@localhost ~]$ ogr2ogr -f "KML" neighborhoods.kml PG:"host=localhost dbname=postgis user=postgres password=123456" -sql "select gid,name,the_geom from neighborhoods"
  (6)批量转换
将postgis中所有的表都导出到mydatadump文件夹下,导出格式是ESRI Shapefile


[postgres@localhost ~]$  ogr2ogr -f "ESRI Shapefile" mydatadump PG:"host=myhost user=myloginname dbname=mydbname password=mypassword"
  部分导出,将指定的表导出到mydatadump中,格式为ESRI Shapefile


[postgres@localhost ~]$ ogr2ogr -f "ESRI Shapefile" mydatadump PG:"host=myhost user=myloginname dbname=mydbname password=mypassword" neighborhood parcels
  (7)ESRI GeoDatabase (*.mdb) ==>PostGIS


[postgres@localhost ~]$ ogr2ogr -f "PostgreSQL" PG:"host=localhost user=someuser dbname=somedb password=somepassword port=5432" C:\GISData\Geonames.mdb -a_srs EPSG:26986
  导入指定的featureclass,重投影,重命名geometry列


[postgres@localhost ~]$ ogr2ogr -f "PostgreSQL" PG:"host=localhost user=someuser dbname=somedb"
/home/postgres/Data/Geonames.mdb GEONAMES_ANNO_HYDRO -a_srs EPSG: 26986 -t_srs EPSG:4269 -nln ma_hydro -lco GEOMETRY_NAME=the_geom_4269
  (8)ESRI Shapefile ==>MySQL


[postgres@localhost ~]$ ogr2ogr -f "MySQL" MYSQL:"mydb,host=myhost,user=mylogin,password=mypassword,port=3306" -nln "world" -a_srs "EPSG:4326" path/to/world_adm0.shp
  (9)Non-spatial Data ==>PostgreSQL


[postgres@localhost ~]$  ogr2ogr -f "PostgreSQL" PG:"host=myserver user=myusername dbname=mydbname password=mypassword" sometable.dbf -nln "sometable"
  下面是如何将shp和tab文件导入到postgis数据库中的命令方式


[postgres@localhost ~]$ ogr2ogr -f PostgreSQL PG:"host=localhost dbname=postgis user=postgres password=850315" sites.shp
[postgres@localhost ~]$ ogr2ogr -f PostgreSQL PG:"host=localhost dbname=postgis user=postgres password=850315" mytabfile.tab
  通过 -nln指定导入数据库中的表名,而不是默认的文件名作为表名


[postgres@localhost ~]$ ogr2ogr -f "PostgreSQL" PG:"host=myhost user=myloginname dbname=mydbname password=mypassword" mytabfile.tab -nln newtablename
  
通过 -a_srs 选项指定输出的投影


[postgres@localhost ~]$ ogr2ogr -f "PostgreSQL" -a_srs "EPSG:2249" PG:"host=myhost user=myloginname dbname=mydbname password=mypassword" mytabfile.tab
  通过-sql 选项后面的SQL语句对postgis中的数据经行筛选后到处到shp文件中

ogr2ogr -f "ESRI Shapefile" mydata.shp PG:"host=myhost user=myloginname dbname=mydbname password=mypassword" -sql "SELECT name, the_geom FROM neighborhoods"
  
可以通过ogrinfo命令来查看元数据


[postgres@localhost ~]$ ogrinfo sites.shp
  此外,也可以通过编程实现数据的导入导出,而且编程实现数据的导出导入灵活性很大
    编程语言:python
    使用到的开发包:ogr,psycopg2
  以后有机会整理一下代码再补充上来,现在事情比较多,而且代码也比较零乱

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-303573-1-1.html 上篇帖子: [Postgres]合并多行到一列(转) 下篇帖子: 使用开源操作系统及数据库系统的可行性分析
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表