def _subtract_layers(self): """ ### Subtract geometry """ # Substract geometry of a layer from the geometry # of one or more different layers. Added mainly # for excluding great lakes from country polygons. for layer in self.layers: if layer.options['subtract-from']: for feat in layer.features: if feat.geom is None: continue cbbox = geom_to_bbox(feat.geom) # We remove it from multiple layers, if wanted. for subid in layer.options['subtract-from']: if subid not in self.layersById: raise KartographError('you want to subtract' + ' from layer "%s" which cannot be found' % subid) for s in self.layersById[subid].features: if s.geom and geom_to_bbox(s.geom).intersects(cbbox): s.subtract_geom(feat.geom) # Finally, we don't want the subtracted features # to be included in our map. layer.features = []
def _init_bounds(self): """ ### Initialize bounding polygons and bounding box ### Compute the projected bounding box """ from geometry.utils import bbox_to_polygon opts = self.options proj = self.proj mode = opts['bounds']['mode'][:] data = opts['bounds']['data'] if 'padding' not in opts['bounds']: padding = 0 else: padding = opts['bounds']['padding'] # If the bound mode is set to *bbox* we simply project # a rectangle in lat/lon coordinates. if mode == "bbox": # catch special case bbox sea = proj.bounding_geometry(data, projected=True) sbbox = geom_to_bbox(sea) sbbox.inflate(sbbox.width * padding) return bbox_to_polygon(sbbox) bbox = BBox() # If the bound mode is set to *points* we project all # points and compute the bounding box. if mode[:5] == "point": ubbox = BBox() for lon, lat in data: pt = proj.project(lon, lat) bbox.update(pt) ubbox.update((lon, lat)) self._unprojected_bounds = ubbox # In bound mode *polygons*, which should correctly be # named gemetry, we compute the bounding boxes of every # geometry. if mode[:4] == "poly": features = self._get_bounding_geometry() ubbox = BBox() if len(features) > 0: for feature in features: ubbox.join(geom_to_bbox(feature.geometry)) feature.project(proj) fbbox = geom_to_bbox(feature.geometry, data["min-area"]) bbox.join(fbbox) # Save the unprojected bounding box for later to # determine what features can be skipped. ubbox.inflate(ubbox.width * padding) self._unprojected_bounds = ubbox else: raise KartographError('no features found for calculating the map bounds') # If we need some extra geometry around the map bounds, we inflate # the bbox according to the set *padding*. bbox.inflate(bbox.width * padding) # At the end we convert the bounding box to a Polygon because # we need it for clipping tasks. return bbox_to_polygon(bbox)
def _crop_layers(self): """ handles crop-to """ for layer in self.layers: if layer.options['crop-to'] is not False: cropped_features = [] for tocrop in layer.features: cbbox = geom_to_bbox(tocrop.geom) crop_at_layer = layer.options['crop-to'] if crop_at_layer not in self.layersById: raise KartographError('you want to substract ' + 'from layer "%s" which cannot be found' % crop_at_layer) for crop_at in self.layersById[crop_at_layer].features: # Sometimes a bounding box may not exist, so get it if not hasattr(crop_at.geom,'bbox'): crop_at.geom.bbox = geom_to_bbox(crop_at.geom) if crop_at.geom.bbox.intersects(cbbox): tocrop.crop_to(crop_at.geom) cropped_features.append(tocrop) layer.features = cropped_features
def _get_view(self): """ ### Initialize the view """ # Compute the bounding box of the bounding polygons. self.src_bbox = bbox = geom_to_bbox(self.bounds_poly) exp = self.options["export"] w = exp["width"] h = exp["height"] ratio = exp["ratio"] # Compute ratio from width and height. if ratio == "auto": ratio = bbox.width / float(bbox.height) # Compute width or heights from ratio. if h == "auto": h = w / ratio elif w == "auto": w = h * ratio return View(bbox, w, h - 1)