// calculate a default cellsize (grid-max.500 rows or columns) // using the input shapefile extent private double calcDefaultCellsize() { MapWinGIS.Shapefile sf = new MapWinGIS.Shapefile(); double result = 0F; if (sf.Open(m_ShpFileName, null)) { double sfX = sf.Extents.xMax - sf.Extents.xMin; double sfY = sf.Extents.yMax - sf.Extents.yMin; double minExt = Math.Max(sfX, sfY); result = Math.Floor(minExt / 250F); } sf.Close(); return result; }
// displays the grid extent! private void cmbExtent_SelectedIndexChanged(object sender, EventArgs e) { MapWinGIS.Shapefile sf = new MapWinGIS.Shapefile(); MapWindow.Interfaces.Layer curLayer; MapWinGIS.Grid grd = new MapWinGIS.Grid(); MapWinGIS.Extents extents; double cellSize = m_DefaultCellSize; //get the cell size to value from textbox Utils.string2double(txtCellSize.Text, out cellSize); //handle the "as specified below" option if (cmbExtent.Text == msgSpecifiedBelow) { txtMinX.Enabled = true; txtMinY.Enabled = true; txtMaxX.Enabled = true; txtMaxY.Enabled = true; return; } else { txtMinX.Enabled = false; txtMinY.Enabled = false; txtMaxX.Enabled = false; txtMaxY.Enabled = false; } //handle the case when shapefile loaded from disk and selected if (cmbExtent.Text == msgSameAs + m_ShortName) { if (sf.Open(m_ShpFileName,null)) { extents = calcShapefileExtent(sf); UpdateExtentBox(extents); } sf.Close(); } for (int i = 0; i < m_MapWin.Layers.NumLayers; ++i) { //an existing layer is used to determine the extent curLayer = m_MapWin.Layers[i]; if (curLayer != null) { if (msgSameAs + curLayer.Name == cmbExtent.Text) { switch (curLayer.LayerType) { //same extention as an existing grid case MapWindow.Interfaces.eLayerType.Grid: grd = curLayer.GetGridObject; UpdateExtentBox(grd.Header); break; //same extention as an existing shapefile case MapWindow.Interfaces.eLayerType.LineShapefile: case MapWindow.Interfaces.eLayerType.PolygonShapefile: case MapWindow.Interfaces.eLayerType.PointShapefile: sf = (MapWinGIS.Shapefile)curLayer.GetObject(); extents = calcShapefileExtent(sf); UpdateExtentBox(extents); break; default: break; } } } } }
private void RotateShapes() { try { System.Collections.SortedList arr = new System.Collections.SortedList(); for (int i = 0; i < g.MapWin.View.SelectedShapes.NumSelected; i++) { arr.Add(g.MapWin.View.SelectedShapes[i].ShapeIndex, g.MapWin.View.SelectedShapes[i].ShapeIndex); } if (arr.Count > 0) { MapWinGIS.Shapefile sf = g.CurrentLayer; if (sf.StartEditingShapes(true, null)) { System.Diagnostics.Debug.WriteLine(sf.get_ErrorMsg(sf.LastErrorCode)); bool allCancelled = false; for (int j = arr.Count - 1; j >= 0; j--) { // Show the dialog to get input -- rotation amount, // rotate about point, etc. Note that dialog defaults // to rotate about the centroid, which is calculated // when the shape is set. // Actual rotation will be done here as well. dlg = new Forms.RotateShapeForm(g); dlg.sf = sf; dlg.Shape = sf.get_Shape((int)arr.GetByIndex(j)); // Note -- don't show modally; we want the user // to be able to click the map to choose a point if needed dlg.Show(g.MapWindowForm); // However, execution should not proceed until the user has finished... // So we use a really old-style waiting scheme while (dlg.Visible) { System.Windows.Forms.Application.DoEvents(); } if (dlg.DialogResult == System.Windows.Forms.DialogResult.Cancel) { allCancelled = true; break; } } if (!allCancelled) { g.CreateUndoPoint(); if (sf.StopEditingShapes(true, true, null) == false) { MapWinUtility.Logger.Message("Failed to save the changes that were made.", "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error, System.Windows.Forms.DialogResult.OK); } } else { sf.StopEditingShapes(false, true, null); MapWinUtility.Logger.Message("Shape resizing has been cancelled - no changes were saved.", "Cancelled", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information, System.Windows.Forms.DialogResult.OK); } } } } catch (System.Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message.ToString()); } }
/// <summary> /// Inserts shapes in the specified table. /// </summary> /// <returns>Number of inserted shapes. -1 if the table doesn't exist.</returns> public int InsertShapes(MapWinGIS.Shapefile sf, string tableName, bool selectedOnly) { return(this.InsertShapes(sf, tableName, this.GetShapefileIndices(sf, selectedOnly))); }
private void Map() { AxMapWinGIS.AxMap map = new AxMapWinGIS.AxMap(); map.Width = 450; map.MouseDownEvent += Map_MouseDownEvent; map.Height = 600; host.Child = map; map.Show(); map.ShapeHighlighted += Map_ShapeHighlighted; map.CreateControl(); map.ShowZoomBar = false; map.ShowCoordinates = MapWinGIS.tkCoordinatesDisplay.cdmNone; map.CursorMode = MapWinGIS.tkCursorMode.cmIdentify; MapWinGIS.Shapefile shapeFileMap = new MapWinGIS.Shapefile(); shapeFileMap.Open(@"D:\Projets\TheManager\TheManager_GUI\bin\Debug\gis\world\World_Countries.shp", null); shapeFileMap.Identifiable = false; map.AddLayer(shapeFileMap, true); map.ZoomToShape(0, 77); MapWinGIS.Shapefile sf = new MapWinGIS.Shapefile(); sf.Identifiable = true; sf.CreateNew("", MapWinGIS.ShpfileType.SHP_POINT); sf.DefaultDrawingOptions.AlignPictureByBottom = false; sf.DefaultDrawingOptions.PointType = MapWinGIS.tkPointSymbolType.ptSymbolStandard; sf.CollisionMode = MapWinGIS.tkCollisionMode.AllowCollisions; List <City> takenCities = new List <City>(); foreach (Journalist journalist in _media.journalists) { double projX = -1; double projY = -1; map.DegreesToProj(journalist.baseCity.Position.Longitude, journalist.baseCity.Position.Latitude, ref projX, ref projY); if (takenCities.Contains(journalist.baseCity)) { projY += Session.Instance.Random(3, 12) / 10.0; } MapWinGIS.Shape shp = new MapWinGIS.Shape(); shp.Create(MapWinGIS.ShpfileType.SHP_POINT); shp.AddPoint(projX, projY); _indexOrders.Add(sf.EditAddShape(shp)); takenCities.Add(journalist.baseCity); } int layer = map.AddLayer(sf, true); foreach (Journalist journalist in _media.journalists) { int handle = map.NewDrawing(MapWinGIS.tkDrawReferenceList.dlScreenReferencedList); double pixX = -1; double pixY = -1; map.DegreesToPixel(journalist.baseCity.Position.Longitude, journalist.baseCity.Position.Latitude, ref pixX, ref pixY); float maxDistance = -1; foreach (Match m in journalist.CommentedGames) { float dist = Utils.Distance(m.home.stadium.city, journalist.baseCity); if (dist > maxDistance) { maxDistance = dist; } } map.DrawCircleEx(handle, pixX, pixY, maxDistance / 2, 2883, true, 25); } map.ShapeIdentified += Map_ShapeIdentified; map.Redraw(); }
/// <summary> /// Generalization of polyline /// shapefiles using the Douglas-Peucker line simplification /// algorithm. This method will output a line shapefile. /// </summary> /// <param name="inFileName">Input shapefile</param> /// <param name="outFileName">Output shapefile</param> /// <param name="tolerance">tolerance parameter - /// specfies the maximum allowed distance between original polyline /// and simplified polyline</param> /// <param name="cback">Use this parameter for reporting progress. Set to null if not needed</param> public static void Generalize(string inFileName, string outFileName, double tolerance, MapWinGIS.ICallback cback) { MapWinGIS.Shapefile oldSF = new MapWinGIS.Shapefile(); if (!oldSF.Open(inFileName, null)) { throw new ArgumentException(string.Format("Shapefile {0} could not be opened. Error: {1}", inFileName, oldSF.get_ErrorMsg(oldSF.LastErrorCode))); } //Check if it's a line shapefile if (!(oldSF.ShapefileType == MapWinGIS.ShpfileType.SHP_POLYLINE || oldSF.ShapefileType == MapWinGIS.ShpfileType.SHP_POLYLINEM || oldSF.ShapefileType == MapWinGIS.ShpfileType.SHP_POLYLINEZ)) { throw new ArgumentException(string.Format("Shapefile {0} must be a polyline shapefile.", inFileName)); } int numShapes = oldSF.NumShapes; int numFields = oldSF.NumFields; //create a new output shapefile MapWinGIS.Shapefile newSF = new MapWinGIS.Shapefile(); MapWinGIS.ShpfileType sftype = MapWinGIS.ShpfileType.SHP_POLYLINE; // if shapefile exists - open it and clear all shapes if (System.IO.File.Exists(outFileName)) { try { //TODO: ask for overwriting.. bool deleted = MapWinGeoProc.DataManagement.DeleteShapefile(ref outFileName); } finally { } } if (!newSF.CreateNew(outFileName, sftype)) { throw new InvalidOperationException ("Error creating shapefile " + outFileName + " " + newSF.get_ErrorMsg(newSF.LastErrorCode)); } newSF.StartEditingShapes(true, cback); //Copy all fields if (!Globals.CopyFields(ref oldSF, ref newSF)) { throw new InvalidOperationException(string.Format("Error copying fields from {0} to {1}", oldSF.Filename, newSF.Filename)); } int newShapeIndex = 0; for (int shpIdx = 0; shpIdx < numShapes; ++shpIdx) { MapWinGIS.Shape shp = oldSF.get_Shape(shpIdx); // convert each part of the polyline shape to a 'geometry' object Geometry geom = MapWinGeoProc.NTS_Adapter.ShapeToGeometry(shp); for (int partIdx = 0; partIdx < geom.NumGeometries; ++partIdx) { Geometry geomPart = (Geometry)geom.GetGeometryN(partIdx); //do the simplification ICoordinate[] oldCoords = geomPart.Coordinates; DouglasPeuckerLineSimplifier simplifier = new DouglasPeuckerLineSimplifier(oldCoords); simplifier.DistanceTolerance = tolerance; ICoordinate[] newCoords = simplifier.Simplify(); //convert the coordinates back to a geometry Geometry newGeom = new LineString(newCoords); //convert the geometry back to a shape MapWinGIS.Shape newShape = MapWinGeoProc.NTS_Adapter.GeometryToShape(newGeom); //add the shape to the new shapefile newShapeIndex = newSF.NumShapes; if (newSF.EditInsertShape(newShape, ref newShapeIndex) == false) { throw new InvalidOperationException("Error inserting shape: " + newSF.get_ErrorMsg(newSF.LastErrorCode)); } //add attribute values for (int fldIdx = 0; fldIdx < numFields; ++fldIdx) { object val = oldSF.get_CellValue(fldIdx, shpIdx); if (newSF.EditCellValue(fldIdx, newSF.NumShapes - 1, val) == false) { throw new InvalidOperationException("Error editing cell value: " + newSF.get_ErrorMsg(newSF.LastErrorCode)); } } } } //close the old shapefile oldSF.Close(); //stop editing and close the new shapefile newSF.StopEditingShapes(true, true, cback); newSF.Close(); }
// displays the grid extent! private void cmbExtent_SelectedIndexChanged(object sender, EventArgs e) { MapWinGIS.Shapefile sf = new MapWinGIS.Shapefile(); MapWindow.Interfaces.Layer curLayer; MapWinGIS.Grid grd = new MapWinGIS.Grid(); MapWinGIS.Extents extents; double cellSize = m_DefaultCellSize; //get the cell size to value from textbox Utils.string2double(txtCellSize.Text, out cellSize); //handle the "as specified below" option if (cmbExtent.Text == msgSpecifiedBelow) { txtMinX.Enabled = true; txtMinY.Enabled = true; txtMaxX.Enabled = true; txtMaxY.Enabled = true; return; } else { txtMinX.Enabled = false; txtMinY.Enabled = false; txtMaxX.Enabled = false; txtMaxY.Enabled = false; } //handle the case when shapefile loaded from disk and selected if (cmbExtent.Text == msgSameAs + m_ShortName) { if (sf.Open(m_ShpFileName, null)) { extents = calcShapefileExtent(sf); UpdateExtentBox(extents); } sf.Close(); } for (int i = 0; i < m_MapWin.Layers.NumLayers; ++i) { //an existing layer is used to determine the extent curLayer = m_MapWin.Layers[i]; if (curLayer != null) { if (msgSameAs + curLayer.Name == cmbExtent.Text) { switch (curLayer.LayerType) { //same extention as an existing grid case MapWindow.Interfaces.eLayerType.Grid: grd = curLayer.GetGridObject; UpdateExtentBox(grd.Header); break; //same extention as an existing shapefile case MapWindow.Interfaces.eLayerType.LineShapefile: case MapWindow.Interfaces.eLayerType.PolygonShapefile: case MapWindow.Interfaces.eLayerType.PointShapefile: sf = (MapWinGIS.Shapefile)curLayer.GetObject(); extents = calcShapefileExtent(sf); UpdateExtentBox(extents); break; default: break; } } } } }
// open a shapefile and populate the list of shapefile fields private void btnOpenFile_Click(object sender, EventArgs e) { MapWinGIS.Shapefile sf = new MapWinGIS.Shapefile(); OpenFileDialog ofdialog = new OpenFileDialog(); ofdialog.Filter = sf.CdlgFilter; ofdialog.Title = "Choose a shapefile"; ofdialog.ShowDialog(this); if (ofdialog.FileName == "") return; lblFileName.Text = ofdialog.FileName; m_ShpFileName = ofdialog.FileName; //create the list of fields and add them to combobox populateFields(m_ShpFileName); }
private void populateFields() { MapWinGIS.Field curField; MapWinGIS.Shapefile sf = new MapWinGIS.Shapefile(); MapWindow.Interfaces.Layer curLayer; cmbFields.Items.Clear(); for (int i = 0; i < m_MapWin.Layers.NumLayers; ++i) { curLayer = m_MapWin.Layers[i]; //the layer type is shapefile (ensured by PopulateLayers) if (curLayer != null) { if (curLayer.Name == cmbLayers.Text) { sf = (MapWinGIS.Shapefile)curLayer.GetObject(); break; } } } //add the fields in the shapefile to the list of fields //for simplification, only numeric fields can be added //because the resulting grid can only contain numeric values for (int i = 0; i < sf.NumFields; ++i) { curField = sf.get_Field(i); if (curField.Type == MapWinGIS.FieldType.DOUBLE_FIELD || curField.Type == MapWinGIS.FieldType.INTEGER_FIELD) { cmbFields.Items.Add(curField.Name); } } if (cmbFields.Items.Count > 0) { cmbFields.SelectedIndex = 0; m_ShpFileName = sf.Filename; } }
private void populateFields(string shpFileName) { cmbFields.Items.Clear(); MapWinGIS.Shapefile sf = new MapWinGIS.Shapefile(); MapWinGIS.Field curField; if (sf.Open(shpFileName,sf.GlobalCallback)) { for (int i = 0; i < sf.NumFields; ++i) { curField = sf.get_Field(i); if (curField.Type == MapWinGIS.FieldType.DOUBLE_FIELD || curField.Type == MapWinGIS.FieldType.INTEGER_FIELD) { cmbFields.Items.Add(curField.Name); } } sf.Close(); } if (cmbFields.Items.Count > 0) { cmbFields.SelectedIndex = 0; } }
//returns a shapefile object corresponding to the map layer name private MapWinGIS.Shapefile findShp(string layerName) { MapWinGIS.Shapefile sf = new MapWinGIS.Shapefile(); MapWindow.Interfaces.Layer curLayer; for (int i = 0; i < m_MapWin.Layers.NumLayers; ++i) { curLayer = m_MapWin.Layers[i]; if (curLayer != null) { if (curLayer.Name == cmbLayers.Text) { sf = (MapWinGIS.Shapefile)curLayer.GetObject(); break; } } } return sf; }