利用SharpMap+OGR添加并显示点

利用SharpMap+OGR添加并显示点

转载自:http://blog.csdn.net/liubx733/article/details/4942239#comments

 

Sharpmap提供了很好的地图渲染功能,其地图显示很漂亮,但是我用的0.9版本尚不支持shp文件编辑和添加、删除功能,因此只好利用OGR进行添加了(删除功能后面再说)。

利用OGR向已经存在的shp文件中写入一个点。

        OSGeo.OGR.Ogr.RegisterAll();//注册

 

        OSGeo.OGR.Driver driver;

        OSGeo.OGR.DataSource ogrds;

OSGeo.OGR.Layer ogrlayer;

Feature ogrfeature;

                        OSGeo.OGR.Geometry pt;

 

                        ogrds = driver.Open(@”New_Shapefile.shp”, 1);//找到文件

                        ogrlayer = ogrds.GetLayerByName(“New_Shapefile”);

                       ogrfeature =
new
Feature(ogrlayer.GetLayerDefn());

                        pt =
new
Geometry(OSGeo.OGR.wkbGeometryType.wkbPoint);

 

                        ogrfeature.SetField(0,“point1”);//属性第一列值为“point1”

                        pt.AddPoint(100 ,100 , 0);//分别为X,Y,Z坐标值,添加点

                        ogrfeature.SetGeometry(pt);

                        ogrlayer.CreateFeature(ogrfeature);

                       

                        ogrfeature.Dispose();销毁

                        ogrds.Dispose();

利用SharpMap进行显示:

                        SharpMap.Data.Providers.ShapeFile shpfile1 =new SharpMap.Data.Providers.ShapeFile(@”New_Shapefile.shp”,true);//direct
, true);

 

                       if (File.Exists(@”New_Shapefile.shp.sidx”))//如果存在索引文件则删除之

                           
File
.Delete(@”New_Shapefile.shp.sidx”);

 

                        shpfile1.Encoding = System.Text.Encoding.GetEncoding(“GB2312”);

                       
if
(mylayer1 != null)//如果存在这个layer则将其移除

                        {

                            myMap.Layers.Remove(mylayer1);

                            mylayer1.Dispose();

                        }

                        mylayer1 =
new
SharpMap.Layers.VectorLayer(“mylayer1”);

                        mylayer1.Style.Symbol =new
Bitmap(@”NETW0019.bmp”);

                        mylayer1.DataSource = shpfile1;

 

                        myMap.Layers.Add(mylayer1);//添加图层

                        myMap.ZoomToExtents();

                        mapImage1.Map = myMap;

                        mapImage1.Refresh();

 

Sharpmap显示地图时,将自动建立一个文件名.shp.sidx文件,如果下一次打开图时已经存在该文件,将自动按其索引进行显示。刚刚添加的点不能够在显示的时候更新,所以在每次显示前判断该文件是否存在,如果存在则删除,这样就可将新添加的点显示出来。

(文中可能有不当的地方,欢迎指教)

 

转载自:https://blog.csdn.net/wsh6759/article/details/7425976

You may also like...