def value(self, val, osm_elem): if osm_elem.geom.type != 'Polygon': return None try: return polylabel(osm_elem.geom) except TopologicalError, ex: log.warn("Polylabel error for %s: %s", osm_elem.osm_id, ex) return None
def generate_details(self, area_file, nr): if self.id in area_file.keys(): entry = area_file[self.id] self.name = entry["name"] self.lat = round(float(entry["center"][0]), 6) self.lon = round(float(entry["center"][1]), 6) else: tags = self._element.get("tags", {}) self.name = tags.get("name", tags.get("official_name", self._default_name.format(nr=nr))) # get name. if not there, get official name. if not there, use default name if isinstance(self.polygon, geometry.MultiPolygon): center_point = self.polygon.centroid else: center_point = polylabel(self.polygon, tolerance=1e-6) self.lat = round(center_point.y, 6) self.lon = round(center_point.x, 6) self.get_feature()
def contours_to_segments(contours, min_diameter): segments = [] thin_segments = [] for i in range(len(contours)): contour1 = contours[i] #find all contours inside inside the current contour contains = [] for j in range(len(contours)): if contour1.contains(contours[j]) and i != j: contains.append(contours[j]) #create a segment of the current contour with its containing contours as holes segment = contour1 for j, cnt in enumerate(contains): # inspect segment segment = segment.difference(cnt) # find visual center (polylabel) of segment label = polylabel(segment, tolerance=1) # determine distance from segment border dist = segment.exterior.distance(Point(label.x, label.y)) # remove segment if the distance from its center is less than an edge size (too thin) if dist < min_diameter: # print("too small contour", i, contour) thin_segments.append({ "polygon": segment, "contains": contains, "diameter": dist, "polylabel": label }) else: segments.append({ "polygon": segment, "contains": contains, "diameter": dist, "polylabel": label }) return segments, thin_segments
'name', element['properties'][filename['attr']]) area = 0.0 # Get polygon for parts in element['geometry']['coordinates']: if element['geometry']['type'] == 'MultiPolygon': pgn = Polygon(parts[0]) else: pgn = Polygon(parts) if area < pgn.area: # Save the polygon with the largest area area = pgn.area maxpgn = pgn try: # The label position is determined by the polylabel geometry = "{\"type\":\"Point\",\"coordinates\":" geometry += str(polylabel( maxpgn, tolerance=10)).replace("(", "[").replace( ")", "]").replace("\'", "\"").replace( "POINT ", "").replace(" ", ",") geometry += "}" except: # In case of InvalidPolygon, use centroid geometry = "{\"type\":\"Point\",\"coordinates\":" geometry += str(maxpgn.centroid).replace( "(", "[").replace(")", "]").replace("\'", "\"").replace( "POINT ", "").replace(" ", ",") geometry += "}" # Set "Geometry" member gjWriter.setGeometry(geometry) else: # Write "Geometry" member(Other than attribute label)
def mappy_polylabel(polygon): if not isinstance(polygon, Polygon): raise TypeError(f"Only polygons are supported, while geometry was {type(pol)}") return polylabel(polygon, tolerance=0.1)