微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Adding SharpMap geometry to a PostGIS database

...or how to upload an ESRI Shapefile to PostGIS.

I've often been asked how to copy data from a shapefile to a PostGISdatabase. PostGIS comes with a commandline-tool for this (shp2pgsql.exe),but all it does is generate a huge sql-textfile that you will need to run afterwards- Not very efficient I think - especially with the ASCII-representation of the geometry. Furthermore I've had several problems with it regarding many international letters in the attributes.

So why not try to letNpgsql and SharpMapdo the job?

I've been working a bit with a small tool that makes it easy to upload an entire shapefile to a PostGreSQL/PostGIS database using SharpMap.

Beloware some of the PostGIS/SharpMaprelated code explained:

First we create a Npgsql connection

The next step is to add a geometry column (see in thefullsource on how you create the table with all the attributes). In this case we set the spatial reference ID to '-1' and name the geometry column 'geom'.

command.CommandText = "SELECT AddGeometryColumn('','myTable','geom','-1','GEOMETRY',2);";
command.ExecuteNonQuery();

Now we are ready to upload to the database,so lets get hold of that shapefile! First we set up a datasource:

sharpmap.Data.Providers.ShapeFile shp = new sharpmap.Data.Providers.ShapeFile(@"C:/data/MyShape.shp",false);

We can Now query all the feature object IDs,by using an extents-query on the full extents:

conn.open();
List<uint> indexes = shp.GetobjectIDsInView(shp.GetExtents());

...and then loop through all the features:

foreach

(uint idx in indexes)
{
sharpmap.Data.FeatureDaTarow
feature = shp.GetFeature(idx);
command.CommandText = "INSERT INTO /"myTable/" (/"geom/") VALUES (GeomFromWKB(:geom,:srid));"
;
command.Parameters.Add(":geom",NpgsqlTypes.NpgsqlDbType
.Bytea);
command.Parameters[":geom"
].Value = feature.Geometry.AsBinary(); //Add the geometry as Well-KNown Binary
command.Parameters.Add(":srid",NpgsqlTypes.NpgsqlDbType
.Integer);
//Set the SRID of the geometry - this must match the SRID we used when the column was created
command.Parameters[":srid"].Value = -1;

//Todo:Addparameters for remaining columns if nessesary (in thatcase alter theINSERTcommandtext accordingly)

command.ExecuteNonQuery();
}
//Clean up
conn.Close();
shp.Close();

...and that is all there is to it !

The great thing about this,is that it is easy to change this to take any other sharpmap datasource and upload as well. And with Christians OGRextension you can suddenly upload a bunch ofdatasource directly to PostGIS.

Download the full source and compiled binaries here: Shape2Pgsql.zip (624,3 KB)(updated April 26,2006)
NpgsqlConnection
conn = new NpgsqlConnection ("Server=localhost;Port=5432;User Id=username;Password=password;Database=myGisDB;")
NpgsqlCommand command = new NpgsqlCommand
();command.Connection = conn;

原文地址:https://www.jb51.cc/postgresql/197408.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐