技术开发 频道

Map Objects2.1 创建shape文件

【IT168技术文档】近来一直修改一个由Map Objects2.1和vs2005 开发的一个桌面gis,设计到了doc文档格式控制和动态生成点或面属性的shape格式的文件。
生成shape文件的思路是先通过AddGeoDataset方法生成文件,然后添加属性数据,下面会在代码中详细解释个参数的含义。
//创建Isoseis private void createIsoseis(MapObjects2.DataConnection dcIso,SeismEClass.SeismEClass tmpse) { try { //blw:::attribute parameters string iso_id = TM.ToString("yyyyMMddHHmmss"); string iso_name = "Isoseis" + iso_id; double iso_longitude = Math.Round(PCenter.X,2); double iso_latitude = Math.Round(PCenter.Y,2); float iso_magnitude = magnitude; string iso_location = poly_pos; string iso_date = TM.Year + "-" + TM.Month + "-" + TM.Day; string iso_depth = angle; int j = 0; for (int i = 6; i < elli.Length;i++ ) { if(elli[i]!=null) { j++; } } //blw:::add shp files MapObjects2.MapLayer temp_iso = getLayer(BasePath + "\\DataLayer\\SDE_Isoseis20070618165431.shp"); MapObjects2.Recordset temp_isorec = temp_iso.Records; MapObjects2.TableDesc temp_isodesc = temp_isorec.TableDesc; MapObjects2.GeoDataset temp_isogeo = dcIso.AddGeoDataset(iso_name ,temp_iso.shapeType ,temp_isodesc , false ,false); MapObjects2.MapLayer temp_iso1 = new MapObjects2.MapLayer(); temp_iso1.GeoDataset = temp_isogeo; MapObjects2.Recordset temp_isorec2=temp_iso1.Records; //blw:::add attributes for (int k = 0; k < j;k++ ) { temp_isorec2.AddNew(); temp_isorec2.Fields.Item("ID").Value = iso_id; temp_isorec2.Fields.Item("LATITUDE").Value = iso_latitude; temp_isorec2.Fields.Item("LONGITUDE").Value = iso_longitude; temp_isorec2.Fields.Item("MAGNITUDE").Value = iso_magnitude; temp_isorec2.Fields.Item("LOCATION").Value = iso_location; temp_isorec2.Fields.Item("DATE_").Value = iso_date; temp_isorec2.Fields.Item("DEPTH").Value = iso_depth; temp_isorec2.Fields.Item("Shape").Value = elli[k+6]; if (k == 0) { temp_isorec2.Fields.Item("AREA_").Value = tmpse.AreaSQVI; temp_isorec2.Fields.Item("IDX").Value = "VI"; } else if (k == 1) { temp_isorec2.Fields.Item("AREA_").Value = tmpse.AreaSQVII; temp_isorec2.Fields.Item("IDX").Value="VII"; } else if (k == 2) { temp_isorec2.Fields.Item("AREA_").Value = tmpse.AreaSQVIII; temp_isorec2.Fields.Item("IDX").Value="VIII"; } else if (k == 3) { temp_isorec2.Fields.Item("AREA_").Value = tmpse.AreaSQIX; temp_isorec2.Fields.Item("IDX").Value = "IX"; } temp_isorec2.Update(); } } catch(Exception ex) { MessageBox.Show(ex.Message); } } 下面是从网络上搜集的一端比较精简的代码,很有代表性。 private void createshapefile() { int i; MapObjects2.DataConnection mapdc=new MapObjects2.DataConnection(); mapdc.Database = "D:\\test"; mapdc.Connect(); MapObjects2.TableDesc mapdes = new MapObjects2.TableDesc(); mapdes.FieldCount = 2; mapdes.set_FieldName(0,"Name"); mapdes.set_FieldType(0, MapObjects2.FieldTypeConstants.moString); mapdes.set_FieldName(1,"Length"); mapdes.set_FieldType(1, MapObjects2.FieldTypeConstants.moDouble); MapObjects2.GeoDataset mapgeo; object zm=false; mapgeo = mapdc.AddGeoDataset("hi", MapObjects2.ShapeTypeConstants.moShapeTypePolygon, mapdes,zm,zm); MapObjects2.MapLayer maplayer=new MapObjects2.MapLayer(); maplayer.GeoDataset = mapgeo; MapObjects2.Recordset maprs=maplayer.Records; for (i = 0; i < 10; i++) { MapObjects2.Polygon mappolygon = new MapObjects2.Polygon(); MapObjects2.Points mappoints = new MapObjects2.Points(); MapObjects2.Point mappoint = new MapObjects2.Point(); mappoint.X = i*100; mappoint.Y = 0; mappoints.Add(mappoint); mappoint.X = (i+1)*100; mappoint.Y = 0; mappoints.Add(mappoint); mappoint.X = (i+1)*100; mappoint.Y = 100; mappoints.Add(mappoint); mappolygon.Parts.Add(mappoints); maprs.AddNew(); maprs.Fields.Item("Shape").Value = mappolygon; maprs.Fields.Item("Name").Value = "hi"; maprs.Fields.Item("Length").Value = 341.4; maprs.Update(); mappolygon = null; mappoints = null; mappoint = null; } }

 

原文地址

0
相关文章