public static MG_Layer LoadShapeFile(string filePath) {// xxx\line.shp MG_Layer mgLayer = new MG_Layer(); try { Ogr.RegisterAll(); DataSource ds = Ogr.Open(filePath, 0); Layer layer = ds.GetLayerByIndex(0); mgLayer.SetLayerName(layer.GetName()); mgLayer.SetLayerType(AsGeometryType(layer.GetGeomType())); Envelope ext = new Envelope(); layer.GetExtent(ext, 1); mgLayer.Extent = AsExtent(ext); // collect data mgLayer.FieldSet = AsFieldSet(layer.GetLayerDefn()); // collect data int fc = layer.GetFeatureCount(1); for (int fid = 0; fid < fc; fid++) { Feature f = layer.GetFeature(fid); MG_Feature mgFeature = AsFeature(f); mgLayer.AddFeature(mgFeature);// collect data } } catch (Exception err) { MessageBox.Show(err.ToString()); } return(mgLayer); }
public void ImportLayer(MG_Layer layer) { if (layer == null) { return; } // stop multi same data if (this.IsTableExist(layer.GetLayerName())) { return; } this.CreateTable(layer.FieldSet, layer.Type); string layerName = layer.FieldSet.GetName(); int fc = layer.GetFeatureCount(); // save ext this.CreateTableExt(); this.InsertExtent(layerName, layer.Extent); for (int i = 0; i < fc; i++) { MG_Feature f = layer.GetFeature(i); if (f.Geometry.Type == layer.Type) { this.Insert(layerName, f.ValueSet, f.Geometry); } } }
public static void CreateMap(MG_Map map, string mapPath) {// c:\\data\\hello.map c:\\data\\layer.shp if (map == null) { return; } // set MapPath map.SetMapPath(mapPath); // save map (layerCount n * layerPath) int count = map.GetLayerCount(); StreamWriter sw = new StreamWriter(mapPath); // stream writer sw.WriteLine(count); // layerCount for (int i = 0; i < count; i++) { string mapFolder = Path.GetDirectoryName(mapPath); MG_Layer layer = map.GetLayer(i); string layerPath = mapFolder + "\\" + layer.GetLayerName() + ".shp"; CreateShapeFile(layer, layerPath); //save layer sw.WriteLine(layerPath); // layerPath } sw.Close(); }
public static void RenderLayer(MG_Layer layer, MG_MapView mapview, Graphics g) { if (layer.GetVisible()) { for (int i = 0; i < layer.GetFeatureCount(); i++) { RenderFeature(layer.GetFeature(i), mapview, g); } } }
public static void CreateShapeFile(MG_Layer mgLayer, string filePath) { string dataSourceName = filePath; string layerName = Path.GetFileNameWithoutExtension(filePath); mgLayer.SetLayerPath(filePath); try { Ogr.RegisterAll(); string driverName = "ESRI Shapefile"; Driver driver = Ogr.GetDriverByName(driverName); if (File.Exists(dataSourceName)) { //System.IO.File.Delete(dataSourceName);// only remove xxx.shp driver.DeleteDataSource(dataSourceName);// reomve xxx.shp dbf shx } DataSource ds = driver.CreateDataSource(dataSourceName, new string[] { }); wkbGeometryType gt = TowkbGeometryType(mgLayer.Type);// retrieve data Layer layer = ds.CreateLayer(layerName, null, gt, new string[] { }); // create three files xxx.shp xxx.dbf xxx.shx // fieldset int fieldCount = mgLayer.FieldSet.Count(); for (int i = 0; i < fieldCount; i++) { MG_Field field = mgLayer.FieldSet.GetAt(i); FieldDefn def = ToFieldDefn(field); layer.CreateField(def, 1); } // feature int featureCount = mgLayer.GetFeatureCount(); for (int i = 0; i < featureCount; i++) { Feature f = ToFeature(mgLayer.GetFeature(i)); layer.CreateFeature(f); f.Dispose(); } // flush data to file layer.Dispose(); ds.Dispose(); driver.Dispose(); } catch (System.Exception ex) { MessageBox.Show(ex.ToString()); } }
public MG_Layer ExportLayer(string table) { MG_Layer mgLayer = new MG_Layer(); mgLayer.SetLayerName(table);// // read ext mgLayer.Extent = this.GetExtent(table); MG_FieldSet fieldSet = this.GetFieldSet(table); mgLayer.FieldSet = fieldSet;// collect data mgLayer.FeatureSet = this.GetFeatureSet(mgLayer.GetFieldSet(), table); mgLayer.Type = mgLayer.FeatureSet.GetAt(0).Geometry.Type; return(mgLayer); }
public MG_BaseTool(MG_ToolType type, MG_Layer layer, MG_MapView mapview) { this.ToolType = type; this.Layer = layer; this.MapView = mapview; if (layer != null) {// pan tool: layer==null this.Feature = new MG_Feature(this.Layer.GetFieldSet()); } // empty point this.FromPoint = Point.Empty; this.ToPoint = Point.Empty; this.Point3 = Point.Empty; this.SelectRect = Rectangle.Empty; }
private DataTable GetDataTable(MG_Layer layer) { // Create a new DataTable. DataTable table = new DataTable(layer.GetLayerName()); // add oid DataColumn oid_Column = new DataColumn("OID", Type.GetType("System.Int32")); table.Columns.Add(oid_Column); int i, j; // Add the column for (i = 0; i < layer.GetFieldSet().Count(); i++) { MG_Field field = layer.GetFieldSet().GetAt(i); DataColumn column = new DataColumn(field.Name); column.DataType = Type.GetType("System.String"); // Add the Column to the DataColumnCollection. table.Columns.Add(column); } // Add the row for (i = 0; i < layer.GetFeatureCount(); i++) { MG_Feature f = layer.GetFeature(i); DataRow row = table.NewRow(); // add oid row[0] = i + 1; for (j = 0; j < f.GetFieldCount(); j++) { object value = f.GetValue(j).Value; if (value != null) { row[j + 1] = value.ToString(); } } // Add the row to the DataRowCollection. table.Rows.Add(row); } return(table); }
public static MG_Map LoadMap(string mapPath) { string mapName = Path.GetFileNameWithoutExtension(mapPath); MG_Map map = new MG_Map(); map.SetMapName(mapName); map.SetMapPath(mapPath); StreamReader sr = new StreamReader(mapPath); // stream reader int count = Int32.Parse(sr.ReadLine()); // layerCount for (int i = 0; i < count; i++) { string layerPath = sr.ReadLine(); // layerPath MG_Layer layer = LoadShapeFile(layerPath); map.AddLayer(layer); } sr.Close(); return(map); }
public void ExportToFile(string table, string filePath) { MG_Layer layer = this.ExportLayer(table); MG_ShapeFileOper.CreateShapeFile(layer, filePath); }
public void ImportToDB(string filePath) { MG_Layer layer = MG_ShapeFileOper.LoadShapeFile(filePath); this.ImportLayer(layer); }
public void SetLayer(MG_Layer layer) { this.layer = layer; }
public MG_ToolDrawPoint(MG_Layer layer, MG_MapView mapview) : base(MG_ToolType.Tool_DrawPoint, layer, mapview) { this.MapPoint = new MG_Point(); }
protected bool IsStart = true; // start end public MG_ToolDrawRectangle(MG_Layer layer, MG_MapView mapview) : base(MG_ToolType.Tool_DrawRectangle, layer, mapview) { }
protected bool QuitEdit = false; //if quit edit linestring public MG_ToolDrawDoubleLineString(MG_Layer layer, MG_MapView mapview) : base(MG_ToolType.Tool_DrawDoubleLineString, layer, mapview) { this.LineString = new MG_LineString(); this.LineStringOther = new MG_LineString(); }
public MG_ToolDrawPolygon(MG_Layer layer, MG_MapView mapview) : base(MG_ToolType.Tool_DrawPolygon, layer, mapview) { this.LineString = new MG_LineString(); this.Polygon = new MG_Polygon(); }