/// <summary> /// The test reprojection. /// </summary> /// <returns> /// The <see cref="bool" />. /// </returns> private static bool TestReprojection() { Debug.Print("\nStart OGR test"); AxMap map = Fileformats.Map; map.Projection = tkMapProjection.PROJECTION_GOOGLE_MERCATOR; var gs = new GlobalSettings(); gs.ShapeInputValidationMode = tkShapeValidationMode.NoValidation; gs.ShapeOutputValidationMode = tkShapeValidationMode.NoValidation; int handle = map.AddLayerFromDatabase(CONNECTION_STRING, "belarus", true); if (handle == -1) { Debug.Print("Failed to open database layer"); } else { Debug.Print("Layer was opened"); OgrLayer l = map.get_OgrLayer(handle); if (l != null) { Debug.Print("Layer was reprojected: " + l.DataIsReprojected); } } 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 int AddFromDatabase(string connectionString, string layerNameOrQuery, bool visible = true) { return(_axMap.AddLayerFromDatabase(connectionString, layerNameOrQuery, visible)); }