def getGeometry(self) -> Geometry: routeGeometry = Geometry(GT_Line) crossFID = self.path[0] crossPointGeom = self.graph.crossObjects[crossFID].point routeGeometry.AddPoint(*crossPointGeom.GetPoint()) for i in range(len(self.path)): if i == 0: continue crossFID = self.path[i] crossFIDPrev = self.path[i - 1] roadFID = self.connector.get((crossFIDPrev, crossFID)) if roadFID is None: roadFID = self.connector.get((crossFID, crossFIDPrev)) roadGeometry = self.graph.roadObjects.get(roadFID).points originalDirection = self.graph.crossGraph[roadFID][ 0] == crossFIDPrev for roadPoint in util.UtilPointIterator( roadGeometry, jump=1 if originalDirection else -1): routeGeometry.AddPoint(*roadPoint) crossPointGeom = self.graph.crossObjects[crossFID].point routeGeometry.AddPoint(*crossPointGeom.GetPoint()) return routeGeometry
def create_wkt(ullon, ullat, lrlon, lrlat): """Create WKT representation using lat/long coordinates. Args: ullon - Upper Left longitude ullat - Upper Left Latitude lrlon - Lower Right Longitude lrlat - Lower Right Latitude Returns: WKT Object from osr """ from ogr import Geometry, wkbLinearRing, wkbPolygon # Create ring ring = Geometry(wkbLinearRing) ring.AddPoint(ullon, ullat) ring.AddPoint(lrlon, ullat) ring.AddPoint(lrlon, lrlat) ring.AddPoint(ullon, lrlat) ring.AddPoint(ullon, ullat) # Create polygon poly = Geometry(wkbPolygon) poly.AddGeometry(ring) return poly.ExportToWkt(), poly
def compare_curve(geometry_x, geometry_y): """Compare whether 2 curve geometries is 'equal'.""" arct = CreateGeometryFromWkt(geometry_x) pgis = CreateGeometryFromWkt(geometry_y) intersection_length = Geometry.Length(Geometry.Intersection(arct, pgis)) arct_length = Geometry.Length(arct) pgis_length = Geometry.Length(pgis) # result = compare_float(intersection_length, arct_length, pgis_length,EPOCH_CURVE) result = compare3float_relative(pgis_length, arct_length, intersection_length, EPOCH_CURVE_RELATIVE) return result
def compare_surface(geometry_x, geometry_y): """Compare whether 2 surface geometries is 'equal'.""" arct = CreateGeometryFromWkt(geometry_x) pgis = CreateGeometryFromWkt(geometry_y) intersection_area = Geometry.Area(Geometry.Intersection(arct, pgis)) arct_area = Geometry.Area(arct) pgis_area = Geometry.Area(pgis) result = compare3float_relative(pgis_area, arct_area, intersection_area, EPOCH_SURFACE_RELATIVE) # result = compare_float(intersection_area, arct_area, pgis_area, EPOCH_SURFACE) return result
def compare_area(geometry_x, geometry_y): """Compare area of 2 geometry types.""" arct = CreateGeometryFromWkt(geometry_x) pgis = CreateGeometryFromWkt(geometry_y) intersection_area = Geometry.Area(Geometry.Intersection(arct, pgis)) arct_area = Geometry.Area(arct) pgis_area = Geometry.Area(pgis) # print('arctern area: %s, postgis area: %s, intersection area: %s' % # (str(arct_area), str(pgis_area), str(intersection_area))) # result = compare_float(intersection_area, arct_area, pgis_area, EPOCH_SURFACE) result = compare3float_relative(pgis_area, arct_area, intersection_area, EPOCH_SURFACE_RELATIVE) return result
def compare_length(geometry_x, geometry_y): """Compare length of 2 geometry types.""" arct = CreateGeometryFromWkt(geometry_x) pgis = CreateGeometryFromWkt(geometry_y) intersection_length = Geometry.Length(Geometry.Intersection(arct, pgis)) arct_length = Geometry.Length(arct) pgis_length = Geometry.Length(pgis) # print('arctern length: %s, postgis length: %s, intersection length: %s' % # (str(arct_length), str(pgis_length), str(intersection_length))) # result = compare_float(intersection_length, arct_length, pgis_length, EPOCH_CURVE) result = compare3float_relative(pgis_length, arct_length, intersection_length, EPOCH_CURVE_RELATIVE) return result
def __init__(self, geometry: ogr.Geometry, withindex=False, jump=1): ''' Takes a point based geometry. Multi Geometries or collections does not work ''' if jump == 0: raise ValueError('Illegal value for jump: 0') self.geometry: ogr.Geometry = geometry.Clone() self.withindex = withindex self.jump = jump
def handle_shape(indent, polygons: List[Polygon], shape: ogr.Geometry, verbose: bool): if verbose: print_shape(indent, shape) try: if isinstance(shape, ogr.Geometry): # Try to get a polygon from the geometry's points gtype = shape.GetGeometryType() if gtype == 2: try: points: List[Point] = shape.GetPoints() if points is not None and len(points) > 0: polygon = Polygon(points) if verbose: print(indent, '→ polygon with ', len(points), 'points') polygons.append(polygon) except Exception as e: print(indent, 'exception making polygon', e) elif gtype in (ogr.wkbPolygon, ogr.wkbMultiPolygon): ... else: print('Unexpected gtype', gtype) sub_count = shape.GetGeometryCount() # print(indent, "sub_count:", sub_count) for sub_idx in range(sub_count): sub_shape: ogr.Geometry = shape.GetGeometryRef(sub_idx) handle_shape(indent + ' ', polygons, sub_shape, verbose) continue else: print(indent, 'It is an unexpected', type(shape)) except Exception as e: print(indent, 'failed!', e) raise