public bool RemoveAllLayers() { AxMap.RemoveAllLayers(); MapControlTools.Layers.Clear(); //MapControlTools.EditingTool.CurrentEditingLayer = null; Events.MapControl_LayerChange layerchange = new Events.MapControl_LayerChange() { LayerChangeReason = Events.LayerChangeReason.RemoveLayer, Layer = null }; On_LayerChange(layerchange); return(true); }
// <summary> // Adds all the shapefiles and images with .tif and .png extentions from the specified folder to the map // </summary> public bool AddLayers(AxMap axMap1, string dataPath) { axMap1.RemoveAllLayers(); axMap1.LockWindow(tkLockMode.lmLock); try { string[] files = Directory.GetFiles(dataPath); foreach (string file in files) { int layerHandle = -1; if (file.ToLower().EndsWith(".shp")) { Shapefile sf = new Shapefile(); if (sf.Open(file, null)) { layerHandle = axMap1.AddLayer(sf, true); } else { MessageBox.Show(sf.ErrorMsg[sf.LastErrorCode]); } } else if (file.ToLower().EndsWith(".tif") || file.ToLower().EndsWith(".png")) { Image img = new Image(); if (img.Open(file, ImageType.TIFF_FILE, false, null)) { layerHandle = axMap1.AddLayer(img, true); } else { MessageBox.Show(img.ErrorMsg[img.LastErrorCode]); } } if (layerHandle != -1) { axMap1.set_LayerName(layerHandle, Path.GetFileName(file)); } } } finally { axMap1.LockWindow(tkLockMode.lmUnlock); Debug.Print("Layers added to the map: " + axMap1.NumLayers); } return(axMap1.NumLayers > 0); }
/// <summary> /// The test deserialize. /// </summary> /// <returns> /// The <see cref="bool" />. /// </returns> private static bool TestDeserialize() { string filename = @"d:\ogrlayer.xml"; using (var reader = new StreamReader(filename)) { string state = reader.ReadToEnd(); var layer = new OgrLayer(); if (!layer.Deserialize(state)) { Debug.Print("Failed to deserialize layer"); return(false); } AxMap map = Fileformats.Map; map.RemoveAllLayers(); map.Projection = tkMapProjection.PROJECTION_WGS84; map.AddLayer(layer, true); } return(true); }
public void CaptureSnapshot() { // clear map _axMap1.ClearDrawings(); _axMap1.RemoveAllLayers(); // create layer CreateLayer(); // var bpp = Screen.PrimaryScreen.BitsPerPixel; DebugMsg($"Current color depth is {bpp} bits per pixel"); // test is only valid when not in 32 bit color depth if (bpp == 32) { DebugMsg(" Test is only valid for color depth less than 32 bpp"); } try { DebugMsg("Calling AxMap.Snapshot() method. Watch for Access Violation Exception if color depth is less than 32 bpp."); var img = _axMap1.SnapShot(_axMap1.Extents); Assert.IsNotNull(img, "axMap1.SnapShot returned null"); DebugMsg($"Successfully called Snapshot() with color depth = {bpp}."); if (bpp != 32) { DebugMsg(" Test verified."); } } catch (AccessViolationException avex) { Assert.Fail(avex.ToString()); } catch (Exception ex) { Assert.Fail(ex.ToString()); } }
/// <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); }
/// <summary> /// Run the open postGIS layers test. /// </summary> /// <param name="textfileLocation"> /// The textfile location. /// </param> /// <param name="theForm"> /// The form. /// </param> /// <returns> /// True on success /// </returns> internal static bool RunOpenPostGisLayers(string textfileLocation, Form1 theForm) { var numErrors = 0; Map = Fileformats.Map; Map.RemoveAllLayers(); // TODO: How to switch between these two: // Map.Projection = tkMapProjection.PROJECTION_WGS84; Map.GrabProjectionFromData = true; var gs = new GlobalSettings(); theForm.Progress("OgrLayerMaxFeatureCount: " + gs.OgrLayerMaxFeatureCount); Application.DoEvents(); theForm.Progress("----------------------- Opening PostGIS layers has started." + Environment.NewLine); // Read testfile: string connectionString; List <string> layersList; if (!ReadTextfile(textfileLocation, out connectionString, out layersList)) { 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 layerName in layersList) { var layer = ds.GetLayerByName(layerName, true); if (layer == null) { continue; } layer.MaxFeatureCount = 10000; layer.GlobalCallback = theForm; theForm.Progress("Opening " + layerName); var handle = Map.AddLayer(layer, true); if (handle == -1) { theForm.WriteError("Failed to add database layer " + layerName + " to the map."); numErrors++; } Application.DoEvents(); } // Close database connection: ds.Close(); theForm.Progress(string.Format("Opening PostGIS layers test has finished, with {0} errors", numErrors)); return(numErrors == 0); }
public virtual void Clear() { _axMap.RemoveAllLayers(); }