protected override void OnMouseDoubleClick(MouseEventArgs e) { base.OnMouseDoubleClick(e); if (m_ActiveTool == Tools.DrawPolygon) { if (GeometryDefined != null) { SharpMap.Geometries.LinearRing ExtRing = new SharpMap.Geometries.LinearRing(); for (int i = 0; i < pointArray.GetUpperBound(0); i++) { ExtRing.Vertices.Add(Map.ImageToWorld(new PointF(pointArray[i].X, pointArray[i].Y))); } ExtRing.Vertices.Add(Map.ImageToWorld(new PointF(pointArray[0].X, pointArray[0].Y))); GeometryDefined(new SharpMap.Geometries.Polygon(ExtRing)); } ActiveTool = Tools.None; } else if (m_ActiveTool == Tools.DrawLine) { if (GeometryDefined != null) { SharpMap.Geometries.LineString Line = new SharpMap.Geometries.LineString(); for (int i = 0; i <= pointArray.GetUpperBound(0); i++) { Line.Vertices.Add(Map.ImageToWorld(new PointF(pointArray[i].X, pointArray[i].Y))); } GeometryDefined(Line); } ActiveTool = Tools.None; } }
internal static SharpMap.Geometries.Polygon ToSharpMapPolygon(GisSharpBlog.NetTopologySuite.Geometries.Polygon polygon) { SharpMap.Geometries.LinearRing exteriorRing = ToSharpMapLinearRing((GisSharpBlog.NetTopologySuite.Geometries.LinearRing)polygon.ExteriorRing); Collection <SharpMap.Geometries.LinearRing> interiorRings = new Collection <SharpMap.Geometries.LinearRing>(); foreach (GisSharpBlog.NetTopologySuite.Geometries.LineString interiorRing in polygon.InteriorRings) { interiorRings.Add(ToSharpMapLinearRing((GisSharpBlog.NetTopologySuite.Geometries.LinearRing)interiorRing)); } return(new SharpMap.Geometries.Polygon(exteriorRing, interiorRings)); }
private static void AddRing(SqlGeometryBuilder builder, SMLinearRing linearRing) { SMPoint pt = linearRing.StartPoint; builder.BeginFigure(pt.X, pt.Y); for (int i = 1; i < linearRing.NumPoints; i++) { pt = linearRing.Point(i); builder.AddLine(pt.X, pt.Y); } builder.EndFigure(); }
/// <summary> /// Converts the <see cref="SharpMap.Geometries.BoundingBox"/> instance <paramref name="boundingBox"/> /// into a correspondant <see cref="SharpMap.Geometries.Polygon"/>. /// </summary> /// <param name="boundingBox"></param> /// <returns></returns> public static SharpMap.Geometries.Geometry ToSharpMapGeometry(SharpMap.Geometries.BoundingBox boundingBox) { Collection <SharpMap.Geometries.Point> vertices = new Collection <SharpMap.Geometries.Point>(); vertices.Add(new SharpMap.Geometries.Point(boundingBox.Min.X, boundingBox.Min.Y)); vertices.Add(new SharpMap.Geometries.Point(boundingBox.Max.X, boundingBox.Min.Y)); vertices.Add(new SharpMap.Geometries.Point(boundingBox.Max.X, boundingBox.Max.Y)); vertices.Add(new SharpMap.Geometries.Point(boundingBox.Min.X, boundingBox.Max.Y)); vertices.Add(new SharpMap.Geometries.Point(boundingBox.Min.X, boundingBox.Min.Y)); SharpMap.Geometries.LinearRing exterior = new SharpMap.Geometries.LinearRing(vertices); return(new SharpMap.Geometries.Polygon(exterior)); }
private static void AddRing(SqlGeometryBuilder builder, SMLinearRing linearRing) { SMPoint pt = linearRing.StartPoint; builder.BeginFigure(pt.X, pt.Y); for (int i = 1; i < linearRing.NumPoints; i++) { pt = linearRing.Point(i); builder.AddLine(pt.X, pt.Y); } builder.EndFigure(); }
/// <summary> /// Returns the box filter string needed in SQL query /// </summary> /// <param name="bbox"></param> /// <returns></returns> private string GetBoxFilterStr(SharpMap.Geometries.BoundingBox bbox) { //geography::STGeomFromText('LINESTRING(47.656 -122.360, 47.656 -122.343)', 4326); SharpMap.Geometries.LinearRing lr = new SharpMap.Geometries.LinearRing(); lr.Vertices.Add(new SharpMap.Geometries.Point(bbox.Left, bbox.Bottom)); lr.Vertices.Add(new SharpMap.Geometries.Point(bbox.Right, bbox.Bottom)); lr.Vertices.Add(new SharpMap.Geometries.Point(bbox.Right, bbox.Top)); lr.Vertices.Add(new SharpMap.Geometries.Point(bbox.Left, bbox.Top)); lr.Vertices.Add(new SharpMap.Geometries.Point(bbox.Left, bbox.Bottom)); SharpMap.Geometries.Polygon p = new SharpMap.Geometries.Polygon(lr); string bboxText = SharpMap.Converters.WellKnownText.GeometryToWKT.Write((SharpMap.Geometries.IGeometry)p); // ""; string whereClause = this.GeometryColumn + ".STIntersects(geometry::STGeomFromText('" + bboxText + "', " + this.SRID.ToString() + ")) = 1"; return(whereClause); // strBbox; }
private static SMGeometry SqlGeometryToSharpMapPolygon(SqlGeometry geometry) { //exterior ring SMLinearRing exterior = new SMLinearRing(GetPoints(geometry.STExteriorRing())); SMLinearRing[] interior = null; if (geometry.STNumInteriorRing() > 0) { interior = new SMLinearRing[(int)geometry.STNumInteriorRing()]; for (int i = 1; i <= geometry.STNumInteriorRing(); i++) { interior[i - 1] = new SMLinearRing(GetPoints(geometry.STInteriorRingN(i))); } } return(new SMPolygon(exterior, interior)); }
private static LinearRing CreateWKBLinearRing(BinaryReader reader, WkbByteOrder byteOrder) { SharpMap.Geometries.LinearRing l = new SharpMap.Geometries.LinearRing(); //l.Vertices.AddRange(ReadCoordinates(reader, byteOrder)); Point[] arrPoint = ReadCoordinates(reader, byteOrder); for (int i = 0; i < arrPoint.Length; i++) { l.Vertices.Add(arrPoint[i]); } //if polygon isn't closed, add the first point to the end (this shouldn't occur for correct WKB data) if (l.Vertices[0].X != l.Vertices[l.Vertices.Count - 1].X || l.Vertices[0].Y != l.Vertices[l.Vertices.Count - 1].Y) { l.Vertices.Add(new Point(l.Vertices[0].X, l.Vertices[0].Y)); } return(l); }
/// <summary> /// Reads and parses the geometry with ID 'oid' from the ShapeFile /// </summary> /// <remarks><see cref="FilterDelegate">Filtering</see> is not applied to this method</remarks> /// <param name="oid">Object ID</param> /// <returns>geometry</returns> private SharpMap.Geometries.Geometry ReadGeometry(uint oid) { brShapeFile.BaseStream.Seek(GetShapeIndex(oid) + 8, 0); //Skip record number and content length ShapeType type = (ShapeType)brShapeFile.ReadInt32(); //Shape type if (type == ShapeType.Null) { return(null); } if (_ShapeType == ShapeType.Point || _ShapeType == ShapeType.PointM || _ShapeType == ShapeType.PointZ) { SharpMap.Geometries.Point tempFeature = new SharpMap.Geometries.Point(); return(new SharpMap.Geometries.Point(brShapeFile.ReadDouble(), brShapeFile.ReadDouble())); } else if (_ShapeType == ShapeType.Multipoint || _ShapeType == ShapeType.MultiPointM || _ShapeType == ShapeType.MultiPointZ) { brShapeFile.BaseStream.Seek(32 + brShapeFile.BaseStream.Position, 0); //skip min/max box SharpMap.Geometries.MultiPoint feature = new SharpMap.Geometries.MultiPoint(); int nPoints = brShapeFile.ReadInt32(); // get the number of points if (nPoints == 0) { return(null); } for (int i = 0; i < nPoints; i++) { feature.Points.Add(new SharpMap.Geometries.Point(brShapeFile.ReadDouble(), brShapeFile.ReadDouble())); } return(feature); } else if (_ShapeType == ShapeType.PolyLine || _ShapeType == ShapeType.Polygon || _ShapeType == ShapeType.PolyLineM || _ShapeType == ShapeType.PolygonM || _ShapeType == ShapeType.PolyLineZ || _ShapeType == ShapeType.PolygonZ) { brShapeFile.BaseStream.Seek(32 + brShapeFile.BaseStream.Position, 0); //skip min/max box int nParts = brShapeFile.ReadInt32(); // get number of parts (segments) if (nParts == 0) { return(null); } int nPoints = brShapeFile.ReadInt32(); // get number of points int[] segments = new int[nParts + 1]; //Read in the segment indexes for (int b = 0; b < nParts; b++) { segments[b] = brShapeFile.ReadInt32(); } //add end point segments[nParts] = nPoints; if ((int)_ShapeType % 10 == 3) { SharpMap.Geometries.MultiLineString mline = new SharpMap.Geometries.MultiLineString(); for (int LineID = 0; LineID < nParts; LineID++) { SharpMap.Geometries.LineString line = new SharpMap.Geometries.LineString(); for (int i = segments[LineID]; i < segments[LineID + 1]; i++) { line.Vertices.Add(new SharpMap.Geometries.Point(brShapeFile.ReadDouble(), brShapeFile.ReadDouble())); } mline.LineStrings.Add(line); } if (mline.LineStrings.Count == 1) { return(mline[0]); } return(mline); } else //(_ShapeType == ShapeType.Polygon etc...) { //First read all the rings List <SharpMap.Geometries.LinearRing> rings = new List <SharpMap.Geometries.LinearRing>(); for (int RingID = 0; RingID < nParts; RingID++) { SharpMap.Geometries.LinearRing ring = new SharpMap.Geometries.LinearRing(); for (int i = segments[RingID]; i < segments[RingID + 1]; i++) { ring.Vertices.Add(new SharpMap.Geometries.Point(brShapeFile.ReadDouble(), brShapeFile.ReadDouble())); } rings.Add(ring); } bool[] IsCounterClockWise = new bool[rings.Count]; int PolygonCount = 0; for (int i = 0; i < rings.Count; i++) { IsCounterClockWise[i] = rings[i].IsCCW(); if (!IsCounterClockWise[i]) { PolygonCount++; } } if (PolygonCount == 1) //We only have one polygon { SharpMap.Geometries.Polygon poly = new SharpMap.Geometries.Polygon(); poly.ExteriorRing = rings[0]; if (rings.Count > 1) { for (int i = 1; i < rings.Count; i++) { poly.InteriorRings.Add(rings[i]); } } return(poly); } else { SharpMap.Geometries.MultiPolygon mpoly = new SharpMap.Geometries.MultiPolygon(); SharpMap.Geometries.Polygon poly = new SharpMap.Geometries.Polygon(); poly.ExteriorRing = rings[0]; for (int i = 1; i < rings.Count; i++) { if (!IsCounterClockWise[i]) { mpoly.Polygons.Add(poly); poly = new SharpMap.Geometries.Polygon(rings[i]); } else { poly.InteriorRings.Add(rings[i]); } } mpoly.Polygons.Add(poly); return(mpoly); } } } else { throw (new ApplicationException("Shapefile type " + _ShapeType.ToString() + " not supported")); } }
internal static GisSharpBlog.NetTopologySuite.Geometries.LinearRing ToNTSLinearRing(SharpMap.Geometries.LinearRing linearRing, GisSharpBlog.NetTopologySuite.Geometries.GeometryFactory factory) { GisSharpBlog.NetTopologySuite.Geometries.Coordinate[] coordinates = new GisSharpBlog.NetTopologySuite.Geometries.Coordinate[linearRing.NumPoints]; int index = 0; foreach (SharpMap.Geometries.Point point in linearRing.Vertices) { coordinates[index++] = ToNTSCoordinate(point, factory); } return(factory.CreateLinearRing(coordinates) as GisSharpBlog.NetTopologySuite.Geometries.LinearRing); }
/// <summary> /// Converts the <see cref="SharpMap.Geometries.BoundingBox"/> instance <paramref name="boundingBox"/> /// into a correspondant <see cref="SharpMap.Geometries.Polygon"/>. /// </summary> /// <param name="boundingBox"></param> /// <returns></returns> public static SharpMap.Geometries.Geometry ToSharpMapGeometry(SharpMap.Geometries.BoundingBox boundingBox) { Collection<SharpMap.Geometries.Point> vertices = new Collection<SharpMap.Geometries.Point>(); vertices.Add(new SharpMap.Geometries.Point(boundingBox.Min.X, boundingBox.Min.Y)); vertices.Add(new SharpMap.Geometries.Point(boundingBox.Max.X, boundingBox.Min.Y)); vertices.Add(new SharpMap.Geometries.Point(boundingBox.Max.X, boundingBox.Max.Y)); vertices.Add(new SharpMap.Geometries.Point(boundingBox.Min.X, boundingBox.Max.Y)); vertices.Add(new SharpMap.Geometries.Point(boundingBox.Min.X, boundingBox.Min.Y)); SharpMap.Geometries.LinearRing exterior = new SharpMap.Geometries.LinearRing(vertices); return new SharpMap.Geometries.Polygon(exterior); }
private static SMGeometry SqlGeometryToSharpMapPolygon(SqlGeometry geometry) { //exterior ring SMLinearRing exterior = new SMLinearRing(GetPoints(geometry.STExteriorRing())); SMLinearRing[] interior = null; if (geometry.STNumInteriorRing()>0) { interior = new SMLinearRing[(int)geometry.STNumInteriorRing()]; for (int i = 0; i < geometry.STNumInteriorRing(); i++) interior[i] = new SMLinearRing(GetPoints(geometry.STInteriorRingN(i))); } return new SMPolygon(exterior, interior); }
/// <summary> /// Reads and parses the geometry with ID 'oid' from the ShapeFile /// </summary> /// <remarks><see cref="FilterDelegate">Filtering</see> is not applied to this method</remarks> /// <param name="oid">Object ID</param> /// <returns>geometry</returns> private SharpMap.Geometries.Geometry ReadGeometry(uint oid) { brShapeFile.BaseStream.Seek(GetShapeIndex(oid) + 8, 0); //Skip record number and content length ShapeType type = (ShapeType)brShapeFile.ReadInt32(); //Shape type if (type == ShapeType.Null) return null; if (_ShapeType == ShapeType.Point || _ShapeType==ShapeType.PointM || _ShapeType==ShapeType.PointZ) { SharpMap.Geometries.Point tempFeature = new SharpMap.Geometries.Point(); return new SharpMap.Geometries.Point(brShapeFile.ReadDouble(), brShapeFile.ReadDouble()); } else if (_ShapeType == ShapeType.Multipoint || _ShapeType == ShapeType.MultiPointM || _ShapeType == ShapeType.MultiPointZ) { brShapeFile.BaseStream.Seek(32 + brShapeFile.BaseStream.Position, 0); //skip min/max box SharpMap.Geometries.MultiPoint feature = new SharpMap.Geometries.MultiPoint(); int nPoints = brShapeFile.ReadInt32(); // get the number of points if (nPoints == 0) return null; for (int i = 0; i < nPoints; i++) feature.Points.Add(new SharpMap.Geometries.Point(brShapeFile.ReadDouble(), brShapeFile.ReadDouble())); return feature; } else if ( _ShapeType == ShapeType.PolyLine || _ShapeType == ShapeType.Polygon || _ShapeType == ShapeType.PolyLineM || _ShapeType == ShapeType.PolygonM || _ShapeType == ShapeType.PolyLineZ || _ShapeType == ShapeType.PolygonZ) { brShapeFile.BaseStream.Seek(32 + brShapeFile.BaseStream.Position, 0); //skip min/max box int nParts = brShapeFile.ReadInt32(); // get number of parts (segments) if (nParts == 0) return null; int nPoints = brShapeFile.ReadInt32(); // get number of points int[] segments = new int[nParts + 1]; //Read in the segment indexes for (int b = 0; b < nParts; b++) segments[b] = brShapeFile.ReadInt32(); //add end point segments[nParts] = nPoints; if ((int)_ShapeType%10 == 3) { SharpMap.Geometries.MultiLineString mline = new SharpMap.Geometries.MultiLineString(); for (int LineID = 0; LineID < nParts; LineID++) { SharpMap.Geometries.LineString line = new SharpMap.Geometries.LineString(); for (int i = segments[LineID]; i < segments[LineID + 1]; i++) line.Vertices.Add(new SharpMap.Geometries.Point(brShapeFile.ReadDouble(), brShapeFile.ReadDouble())); mline.LineStrings.Add(line); } if (mline.LineStrings.Count == 1) return mline[0]; return mline; } else //(_ShapeType == ShapeType.Polygon etc...) { //First read all the rings List<SharpMap.Geometries.LinearRing> rings = new List<SharpMap.Geometries.LinearRing>(); for (int RingID = 0; RingID < nParts; RingID++) { SharpMap.Geometries.LinearRing ring = new SharpMap.Geometries.LinearRing(); for (int i = segments[RingID]; i < segments[RingID + 1]; i++) ring.Vertices.Add(new SharpMap.Geometries.Point(brShapeFile.ReadDouble(), brShapeFile.ReadDouble())); rings.Add(ring); } bool[] IsCounterClockWise = new bool[rings.Count]; int PolygonCount = 0; for (int i = 0; i < rings.Count;i++) { IsCounterClockWise[i] = rings[i].IsCCW(); if (!IsCounterClockWise[i]) PolygonCount++; } if (PolygonCount == 1) //We only have one polygon { SharpMap.Geometries.Polygon poly = new SharpMap.Geometries.Polygon(); poly.ExteriorRing = rings[0]; if (rings.Count > 1) for (int i = 1; i < rings.Count; i++) poly.InteriorRings.Add(rings[i]); return poly; } else { SharpMap.Geometries.MultiPolygon mpoly = new SharpMap.Geometries.MultiPolygon(); SharpMap.Geometries.Polygon poly = new SharpMap.Geometries.Polygon(); poly.ExteriorRing = rings[0]; for (int i = 1; i < rings.Count;i++) { if (!IsCounterClockWise[i]) { mpoly.Polygons.Add(poly); poly = new SharpMap.Geometries.Polygon(rings[i]); } else poly.InteriorRings.Add(rings[i]); } mpoly.Polygons.Add(poly); return mpoly; } } } else throw (new ApplicationException("Shapefile type " + _ShapeType.ToString() + " not supported")); }
public Main() { InitializeComponent(); _sharpMap = new SharpMap.Map(this._sharpMapImage.Size); _sharpMap.BackColor = Color.White; this._manager = new LayerManager(this._sharpMap); this.select = new SharpMap.Geometries.LinearRing(); this.poligon = new List<Point>(); this._applicationMode = AvailableModes.ImagePan; this._selectionDownCoordinate = new Point(); this._selectionLastDownCoordinate = new Point(); this._backupImage = null; this._polySelection = false; select = new SharpMap.Geometries.LinearRing(); _sharpMap = new SharpMap.Map(this.pbxMapa.Size); _sharpMap.BackColor = Color.WhiteSmoke; /*String path; _sharpMap = new SharpMap.Map(this._sharpMapImage.Size); _sharpMap.BackColor = Color.White; this._manager = new LayerManager(this._sharpMap); RefreshMap(); } public void RefreshMap() { if (this._sharpMap.Size.Width == 0 || this._sharpMap.Size.Height == 0) { this._sharpMapImage.Image = null; } else { if (_sharpMap.Layers.Count != 0) this._sharpMapImage.Image = _sharpMap.GetMap(); else this._sharpMapImage.Image = null; } if (_sharpMap.Layers.Count != 0) this._sharpMapImage.Image = _sharpMap.GetMap(); else this._sharpMapImage.Image = null; } private void btnRemoveLayer_Click(object sender, EventArgs e) { if (this._dataGridLayers.SelectedRows.Count > 0) { if (this._dataGridLayers.SelectedRows[0].Index >= 0) { _sharpMap.Layers.RemoveAt(this._dataGridLayers.SelectedRows[0].Index); this._dataGridLayers.Rows.RemoveAt(this._dataGridLayers.SelectedRows[0].Index); if (_sharpMap.Layers.Count != 0) { _sharpMap.ZoomToExtents(); } RefreshMap(); } } } private void menyAddVectorLayer_Click(object sender, EventArgs e) { String path; OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "shp Files (.shp)|*.shp|All Files (*.*)|*.*"; dialog.FilterIndex = 1; dialog.Multiselect = false; DialogResult res = dialog.ShowDialog(); if (res == DialogResult.OK) path = dialog.FileName; else path = DATA_PATH; //SharpMap.Layers.VectorLayer layer = new SharpMap.Layers.VectorLayer(DATA_NAME); //layer.DataSource = new SharpMap.Data.Providers.ShapeFile(DATA_PATH); SharpMap.Layers.VectorLayer layer = new SharpMap.Layers.VectorLayer(dialog.SafeFileName); layer.DataSource = new SharpMap.Data.Providers.ShapeFile(path); layer.Style.Fill = Brushes.LightGreen; layer.Style.EnableOutline = true; layer.Style.Outline = Pens.DarkGreen;*/ //dodavanje labele /*SharpMap.Layers.LabelLayer labelLayer = new SharpMap.Layers.LabelLayer("Country Names"); labelLayer.DataSource = layer.DataSource; labelLayer.LabelColumn = "NAME"; labelLayer.Style.CollisionDetection = true; //labelLayer.Style.CollisionBuffer = new SizeF(10, 10); labelLayer.LabelFilter = SharpMap.Rendering.LabelCollisionDetection.ThoroughCollisionDetection; labelLayer.MultipartGeometryBehaviour = SharpMap.Layers.LabelLayer.MultipartGeometryBehaviourEnum.Largest; labelLayer.Style.Font = new Font(FontFamily.GenericSansSerif, 8); _sharpMap.Layers.Add(labelLayer);*/ /* _sharpMap.Layers.Add(layer); this.lbxLayers.Items.Add(layer.LayerName); _sharpMap.ZoomToExtents();*/ RefreshMap(); }
/// <summary> /// Returns the box filter string needed in SQL query /// </summary> /// <param name="bbox"></param> /// <returns></returns> private string GetBoxFilterStr(SharpMap.Geometries.BoundingBox bbox) { //geography::STGeomFromText('LINESTRING(47.656 -122.360, 47.656 -122.343)', 4326); SharpMap.Geometries.LinearRing lr = new SharpMap.Geometries.LinearRing(); lr.Vertices.Add(new SharpMap.Geometries.Point(bbox.Left, bbox.Bottom)); lr.Vertices.Add(new SharpMap.Geometries.Point(bbox.Right, bbox.Bottom)); lr.Vertices.Add(new SharpMap.Geometries.Point(bbox.Right, bbox.Top)); lr.Vertices.Add(new SharpMap.Geometries.Point(bbox.Left, bbox.Top)); lr.Vertices.Add(new SharpMap.Geometries.Point(bbox.Left, bbox.Bottom)); SharpMap.Geometries.Polygon p = new SharpMap.Geometries.Polygon(lr); string bboxText = SharpMap.Converters.WellKnownText.GeometryToWKT.Write((SharpMap.Geometries.IGeometry)p); // ""; string whereClause = this.GeometryColumn + ".STIntersects(geometry::STGeomFromText('" + bboxText + "', " + this.SRID.ToString() + ")) = 1"; return whereClause; // strBbox; }