public static void ImportOgrLayer() { int layerHandle = App.Legend.SelectedLayer; if (layerHandle == -1) { return; } var sf = App.Map.get_Shapefile(layerHandle); if (sf == null) { MessageHelper.Info("Selected layer is not a vector layer."); return; } using (var form = new OgrConnectionForm()) { if (form.ShowDialog(MainForm.Instance) == DialogResult.OK) { var ds = new OgrDatasource(); if (!OgrHelper.OpenDatasource(ds, form.ConnectionParams)) { return; } string layerName = App.Map.get_LayerName(layerHandle); layerName = layerName.Replace(".", "_"); using (var importForm = new OgrImportShapefile(layerName)) { if (importForm.ShowDialog(MainForm.Instance) == DialogResult.OK) { layerName = importForm.LayerName; if (!ds.ImportShapefile(sf, layerName, "", tkShapeValidationMode.NoValidation)) { MessageHelper.Warn("Failed to import shapefile: " + ds.GdalLastErrorMsg); } else { MessageHelper.Info("Layer was imported: " + layerName); } } } } } }
private static bool ImportShapefilesFromFolder() { var ds = new OgrDatasource(); if (!ds.Open(CONNECTION_STRING)) { Debug.Print("Failed to establish connection: " + ds.GdalLastErrorMsg); } else { string path = @"d:\data\sf\london"; var files = Directory.GetFiles(path, "*.shp"); foreach (var file in files) { var sf = new Shapefile(); if (!sf.Open(file)) { Debug.Print("Failed to open shapefile: {0}\n{1}", file, sf.ErrorMsg[sf.LastErrorCode]); } else { string name = Path.GetFileNameWithoutExtension(file); if (!ds.ImportShapefile(sf, name, "OVERWRITE=YES", tkShapeValidationMode.NoValidation)) { Debug.Print("Failed to import shapefile: " + name); } else { Debug.Print("Layer was imported: " + name); var layer = ds.GetLayerByName(name); if (layer != null) { Debug.Print("Imported features count: " + layer.FeatureCount); layer.Close(); } } } } ds.Close(); } return(true); }
/// <summary> /// Import shapefiles into the PostGIS database /// </summary> /// <param name="textfileLocation"> /// The textfile location. /// </param> /// <param name="theForm"> /// The form. /// </param> /// <returns> /// The <see cref="bool"/>. /// </returns> internal static bool RunPostGisImportSf(string textfileLocation, Form1 theForm) { var numErrors = 0; Map = Fileformats.Map; Map.RemoveAllLayers(); Map.Projection = tkMapProjection.PROJECTION_WGS84; Application.DoEvents(); theForm.Progress("----------------------- Importing of shapefiles has started." + Environment.NewLine); // Read testfile: string connectionString; List <string> shapefileList; if (!ReadTextfile(textfileLocation, out connectionString, out shapefileList)) { throw new Exception("Cannot read text file"); } // Connect to data source: var ds = new OgrDatasource { GlobalCallback = theForm }; if (!ds.Open(connectionString)) { throw new Exception("Failed to open datasource: " + ds.GdalLastErrorMsg); } // Get queries: foreach (var shapefileLocation in shapefileList) { var layerName = Path.GetFileNameWithoutExtension(shapefileLocation); if (!File.Exists(shapefileLocation)) { theForm.WriteError(shapefileLocation + " does not exists. Skipping"); continue; } // Open shapefile: theForm.Progress(string.Format("Reading {0} shapefile", layerName)); var fm = new FileManager(); var sf = fm.OpenShapefile(shapefileLocation, theForm); theForm.Progress(string.Format("Importing {0} shapefile", layerName)); if (!ds.ImportShapefile(sf, layerName, "OVERWRITE=YES", tkShapeValidationMode.NoValidation)) { var errorMsg = fm.ErrorMsg[fm.LastErrorCode]; // let's check GDAL error as well var gs = new GlobalSettings(); errorMsg += " GDAL error message: " + gs.GdalLastErrorMsg; theForm.WriteError( string.Format("Error importing shapefile [{0}]: {1}", shapefileLocation, errorMsg)); numErrors++; } else { // Read layer and add to map: theForm.Progress(string.Format("Reading {0} layer from db", layerName)); var handle = Map.AddLayerFromDatabase(connectionString, layerName, true); if (handle == -1) { theForm.WriteError("Failed to open database layer: " + layerName); numErrors++; } Application.DoEvents(); } // Close shapefile: sf.Close(); } // Close database connection: ds.Close(); theForm.Progress(string.Format("Importing of shapefiles test has finished, with {0} errors", numErrors)); return(numErrors == 0); }
public bool ImportLayer(IFeatureSet featureSet, string layerName, string creationOptions = "", ValidationMode validationMode = ValidationMode.TryFixSkipOnFailure) { return(_datasource.ImportShapefile(featureSet.GetInternal(), layerName, creationOptions, (tkShapeValidationMode)validationMode)); }