For this second Snippet Wednesday, I wanted to share the functions that I use a lot to convert between VIKTOR Geopolylines and polygons and shapely.
Shapely is a very powerful geometry package, especially in 2D applications it can be very strong. It allows you to easily perform operations such as finding overlap between polygons, interpolating points every n meters along a line… This makes it especially useful in GIS-like applications, and so I have use it a lot on dyke projects such as for instance the hdsr piping tool. I hope it is also useful to you!
from shapely.geometry import Point, LineString, Polygon from viktor.geometry import GeoPoint, GeoPolyline, GeoPolygon def convert_geo_point_to_shapeply_point(geo_point: GeoPoint) -> Point: """Convert a VIKTOR SDK GeoPoint in RD into a shapely Point""" return Point(geo_point.rd) def convert_geo_polyline_to_linestring(geo_polyline: GeoPolyline) -> LineString: """Convert a VIKTOR SDK GeoPolyline in RD into a shapely LineString""" list_points = [pt.rd for pt in geo_polyline.points] return LineString(list_points) def convert_geopolygon_to_shapely_polgon(geopolygon: GeoPolygon) -> Polygon: """Convert a VIKTOR SDK GeoPolygon in RD into a shapely Polygon""" return Polygon([points.rd for points in geopolygon.points])
from shapely import LineString, Polygon, Point from viktor.geometry import GeoPoint, GeoPolygon, Geopolyline def convert_shapely_point_to_geo_point(point: Point) -> GeoPoint: """Convert a shapely Point in RD coordinates into a VIKTOR GeoPoint""" return GeoPoint.from_rd(*list(point.coords)) def convert_linestring_to_geo_polyline(linestring: LineString) -> GeoPolyline: """Convert a shapely LineString in RD coordinates into a VIKTOR GeoPolyline""" linestring_points = list(linestring.coords) return GeoPolyline(*[GeoPoint.from_rd(pt) for pt in linestring_points]) def convert_shapely_polygon_to_geopolygon(polygon: Polygon) -> GeoPolygon: """Convert a shapely polygon in RD coordinates into a VIKTOR GeoPolygon""" return GeoPolygon(*[GeoPoint.from_rd(point) for point in polygon.exterior.coords])