/// <summary> /// Converts a <b>Point</b> to a <see cref="System.Drawing.PointF">PointF</see>. /// </summary> /// <param name="point">The <b>Point</b> to convert.</param> /// <returns>A <see cref="System.Drawing.PointF">PointF</see> with the same values x and y values as <i>point</i>.</returns> public static PointF PointToPointF(Point point) { return(new PointF((float)point.getX(), (float)point.getY())); }
/// <summary> /// Converts a <b>Geometry</b> object to its representation as a <see cref="System.Drawing.Drawing2D.GraphicsPath">GraphicsPath</see>. /// </summary> /// <param name="geometry">The <b>Geometry</b> object.</param> /// <returns>The <see cref="System.Drawing.Drawing2D.GraphicsPath">GraphicsPath</see> representation of the supplied <b>Geometry</b>.</returns> /// <exception cref="ArgumentNullException"><i>geometry</i> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="InvalidOperationException"><i>geometry</i> cannot be converted to a <see cref="System.Drawing.Drawing2D.GraphicsPath">GraphicsPath</see>.</exception> public static GraphicsPath GeometryToGraphicsPath(Geometry geometry) { if (geometry == null) { throw new ArgumentNullException("geometry"); } GraphicsPath path = new GraphicsPath(); if (geometry is GeometryCollection) { GeometryCollection geometries = (GeometryCollection)geometry; for (int i = 0; i < geometries.getNumGeometries(); i++) { path.AddPath(GeometryToGraphicsPath(geometries.getGeometryN(i)), false); } } else if (geometry is Point) { Point point = (Point)geometry; path.AddRectangle(new RectangleF((float)point.getX() - .5f, (float)point.getY() - .5f, 1, 1)); } else if (geometry is Polygon) { Polygon polygon = (Polygon)geometry; path.AddPath(GeometryToGraphicsPath(polygon.getExteriorRing()), true); for (int i = 0; i < polygon.getNumInteriorRing(); i++) { path.AddPath(GeometryToGraphicsPath(polygon.getInteriorRingN(i)), false); } } else if (geometry is LinearRing) { Coordinate[] coords = geometry.getCoordinates(); PointF[] points = new PointF[coords.Length]; for (int i = 0; i < points.Length; i++) { PointF point = new PointF((float)coords[i].x, (float)coords[i].y); points[i] = point; } path.AddPolygon(points); } else if (geometry is LineString) { Coordinate[] coords = geometry.getCoordinates(); PointF[] points = new PointF[coords.Length]; for (int i = 0; i < points.Length; i++) { PointF point = new PointF((float)coords[i].x, (float)coords[i].y); points[i] = point; } path.AddLines(points); } else { throw new InvalidOperationException("Cannot convert this to a GDI Graphics path"); } Debug.Assert(path != null, "Should not return null."); return(path); }
/// <summary> /// Converts a <b>Geometry</b> object to its projected representation as a <see cref="System.Drawing.Drawing2D.GraphicsPath">GraphicsPath</see>. /// </summary> /// <param name="geometry">The <b>Geometry</b> object.</param> /// <param name="projection">The projection to use.</param> /// <returns>The <see cref="System.Drawing.Drawing2D.GraphicsPath">GraphicsPath</see> representation of the supplied <b>Geometry</b>.</returns> /// <exception cref="ArgumentNullException"><i>geometry</i> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="InvalidOperationException"><i>geometry</i> cannot be converted to a <see cref="System.Drawing.Drawing2D.GraphicsPath">GraphicsPath</see>.</exception> public static GraphicsPath ProjectGeometryToGraphicsPath(Geometry geometry, ICoordinateTransformation projection) { if (geometry == null) { throw new ArgumentNullException("geometry"); } GraphicsPath path = new GraphicsPath(); if (geometry is GeometryCollection) { GeometryCollection geometries = (GeometryCollection)geometry; for (int i = 0; i < geometries.getNumGeometries(); i++) { path.AddPath(GeometryHelper.ProjectGeometryToGraphicsPath(geometries.getGeometryN(i), projection), false); } } else if (geometry is Point) { Point point = (Point)geometry; PointF point2 = new PointF((float)point.getX(), (float)point.getY()); if (projection != null) { point2 = ProjectionHelper.Project(point2, projection); } float x = point2.X; float y = point2.Y; //HACK: need to determine this 15,000 number path.AddRectangle(new RectangleF(x, y, 15000, 15000)); } else if (geometry is Polygon) { Polygon polygon = (Polygon)geometry; path.AddPath(GeometryHelper.ProjectGeometryToGraphicsPath(polygon.getExteriorRing(), projection), true); for (int i = 0; i < polygon.getNumInteriorRing(); i++) { path.AddPath(GeometryHelper.ProjectGeometryToGraphicsPath(polygon.getInteriorRingN(i), projection), false); } } else if (geometry is LinearRing) { Coordinate[] coords = geometry.getCoordinates(); PointF[] points = new PointF[coords.Length]; bool project = projection != null; for (int i = 0; i < points.Length; i++) { PointF point = new PointF((float)coords[i].x, (float)coords[i].y); if (project) { point = ProjectionHelper.Project(point, projection); } points[i] = point; } path.AddPolygon(points); } else if (geometry is LineString) { Coordinate[] coords = geometry.getCoordinates(); PointF[] points = new PointF[coords.Length]; bool project = projection != null; for (int i = 0; i < points.Length; i++) { PointF point = new PointF((float)coords[i].x, (float)coords[i].y); if (project) { point = ProjectionHelper.Project(point, projection); } points[i] = point; } path.AddLines(points); } else { throw new InvalidOperationException("Cannot convert this to a GDI Graphics path"); } Debug.Assert(path != null, "Should not return null."); return(path); }