Shapefile文件是ArcGIS存儲矢量要素的標准格式,要讀寫Shapefile最簡單的方法當然是基於ArcObject(或者ArcEngine)開發,不過網上也有一些開源的解譯Shapefile的代碼都是值得參考的,lz曾經用到過一個,源碼已經貼到下邊,有興趣的可以下載看看(來源已經記不清了,如果這是您的代碼請聯系我),下邊是兩種方法的代碼,其實代碼很簡單,但由於經常會用到所以記下來以便日后查閱。直接上代碼。
打開Shapefile:
public static IFeatureClass GetShpFile(string filePath, string fileName) { IFeatureWorkspace featureWorkspace; IFeatureClass featureClass; featureWorkspace = GetShapeWorkspace(filePath) as IFeatureWorkspace; try { featureClass = featureWorkspace.OpenFeatureClass(fileName); } catch { featureClass = null; } System.Runtime.InteropServices.Marshal.ReleaseComObject(featureWorkspace); return featureClass; }
public static IWorkspace GetShapeWorkspace(string filePath) { IWorkspace workspace; IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass(); workspace = workspaceFactory.OpenFromFile(filePath, 0); System.Runtime.InteropServices.Marshal.ReleaseComObject(workspaceFactory); workspaceFactory = null; return workspace; }
向Shapefile中添加要素(一點元素為例):
public bool AddPointsToLayer(ILayer pLayer, List<IPoint> listPoint) { IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer; if (pFeatureLayer == null) { System.Windows.Forms.MessageBox.Show(pLayer.Name + "不是矢量圖層!"); return false; } IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass; if (pFeatureClass.ShapeType != esriGeometryType.esriGeometryPoint) { System.Windows.Forms.MessageBox.Show(pLayer.Name + "不是點圖層!"); return false; } IFeatureCursor pFeatureCursor = pFeatureClass.Insert(true); IFeatureBuffer pFeatureBuffer = null; foreach (var p in listPoint) { pFeatureBuffer = pFeatureClass.CreateFeatureBuffer(); IFeature pNewFeature = pFeatureBuffer as IFeature; pNewFeature.Shape = p as IGeometry; pNewFeature.set_Value(pNewFeature.Fields.FindField("Name"), "point1"); //pNewFeature.Store(); pFeatureCursor.InsertFeature(pFeatureBuffer); } pFeatureCursor.Flush(); return true; }
讀取Shapefile的開源代碼點擊這里下載,下邊是讀取代碼:
private void openShapeFile() { //Create the dialog allowing the user to select the "*.shp" and the "*.dbf" files OpenFileDialog ofd = new OpenFileDialog(); ofd.Multiselect = true; if (!(ofd.ShowDialog() ?? false)) return; //Get the file info objects for the SHP and the DBF file selected by the user FileInfo shapeFile = null; FileInfo dbfFile = null; foreach (FileInfo fi in ofd.Files) { if (fi.Extension.ToLower() == ".shp") { shapeFile = fi; } if (fi.Extension.ToLower() == ".dbf") { dbfFile = fi; } } //Read the SHP and DBF files into the ShapeFileReader ShapeFile shapeFileReader = new ShapeFile(); if (shapeFile != null && dbfFile != null) { shapeFileReader.Read(shapeFile, dbfFile); } else { HtmlPage.Window.Alert("Please select a SP and a DBF file to proceed."); return; } //Add the shapes from the shapefile into a graphics layer named "shapefileGraphicsLayer" //the greaphics layer should be present in the XAML or created earlier GraphicsLayer graphicsLayer = MyMap.Layers["shapefileGraphicsLayer"] as GraphicsLayer; foreach (ShapeFileRecord record in shapeFileReader.Records) { Graphic graphic = record.ToGraphic(); if (graphic != null) graphicsLayer.Graphics.Add(graphic); } }
直接拖拽打開:
private void MyMap_Drop(object sender, DragEventArgs e) { try { //獲取拖放到地圖上的文件信息 IDataObject dataObject = e.Data as IDataObject; FileInfo[] files = dataObject.GetData(DataFormats.FileDrop) as FileInfo[]; //判斷拖放的文件是否為.shp和.dbf FileInfo shapeFile = null; FileInfo dbfFile = null; foreach (FileInfo fi in files) { if (fi.Extension.ToLower() == ".shp") shapeFile = fi; if (fi.Extension.ToLower() == ".dbf") dbfFile = fi; } // 讀取Shapefile數據 ShapeFile shapeFileReader = new ShapeFile(); if (shapeFile != null && dbfFile != null) { shapeFileReader.Read(shapeFile, dbfFile); } else { MessageBox.Show("請將.dbf和.shp文件同時拖放到地圖上!"); return; } IList<Graphic> lstGraphics = new List<Graphic>(); foreach (ShapeFileRecord record in shapeFileReader.Records) { //將從Shapefile中讀取的記錄轉換為Graphic Graphic graphic = record.ToGraphic(); //若沒有空間參考將會報錯,默認設置為地圖參考 if (graphic.Geometry.SpatialReference == null) graphic.Geometry.SpatialReference = MyMap.SpatialReference; if (graphic != null) lstGraphics.Add(graphic); } // 如果空間參考不一致,可能需要投影 if (lstGraphics.Count > 0) { GeometryService projectTask = new GeometryService("http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer"); projectTask.ProjectCompleted += new EventHandler<GraphicsEventArgs>(projectTask_ProjectCompleted); projectTask.Failed += new EventHandler<TaskFailedEventArgs>(projectTask_Failed); //將平面坐標轉換為經緯度 projectTask.ProjectAsync(lstGraphics, MyMap.SpatialReference); } } catch (Exception ex) { MessageBox.Show("拖放文件錯誤:" + ex.ToString()); } }