Ejemplo n.º 1
0
 def plot_polygon(self, polygon, inverse = False):
     ext = self.plot_linear_ring(polygon.exterior, truncate=True, inverse = inverse)
     if len(ext) == 1:
         pts_int = []
         for interior in polygon.interiors:
             pts_int += self.plot_linear_ring(interior, truncate=True, inverse = inverse)
         return [Polygon(ext[0], pts_int)]
     elif len(ext) == 0:
         return []
     else:
         raise KartographError('unhandled case: exterior is split into multiple rings')
Ejemplo n.º 2
0
 def project_polygon(self, polygon):
     ext = self.project_linear_ring(polygon.exterior)
     if len(ext) == 1:
         pts_int = []
         for interior in polygon.interiors:
             pts_int += self.project_linear_ring(interior)
         return [Polygon(ext[0], pts_int)]
     elif len(ext) == 0:
         return []
     else:
         raise KartographError(
             'unhandled case: exterior is split into multiple rings')
Ejemplo n.º 3
0
 def _render_geometry(self, geometry):
     from shapely.geometry import Polygon, MultiPolygon, LineString, MultiLineString, Point
     if geometry is None:
         return
     if isinstance(geometry, (Polygon, MultiPolygon)):
         return self._render_polygon(geometry)
     elif isinstance(geometry, (LineString, MultiLineString)):
         return self._render_line(geometry)
     elif isinstance(geometry, (Point)):
         return self._render_point(geometry)
     else:
         raise KartographError('svg renderer doesn\'t know how to handle ' +
                               str(type(geometry)))
Ejemplo n.º 4
0
    def _render_feature(self,
                        feature,
                        attributes=[],
                        labelOpts=False,
                        offset={
                            'x': 0,
                            'y': 0
                        },
                        scale=1.,
                        is_county=False):
        if feature.geometry is None:
            print 'Geometry of {0} is none!'.format(feature.properties['NAME'])
            return None
        node = self._render_geometry(feature.geometry, offset, scale,
                                     is_county)
        if node is None:
            print 'node is None!!!!'
            return None

        if attributes == 'all':
            attributes = []
            for k in feature.props:
                attributes.append(dict(src=k, tgt=k))

        for cfg in attributes:
            if 'src' in cfg:
                tgt = re.sub('(\W|_)+', '-', cfg['tgt'].lower())
                if cfg['src'] not in feature.props:
                    continue
                    #raise KartographError(('attribute not found "%s"'%cfg['src']))
                val = feature.props[cfg['src']]
                if isinstance(val, (int, float)):
                    val = str(val)
                node.setAttribute('data-' + tgt, val)
                if tgt == "id":
                    node.setAttribute('id', val)

            elif 'where' in cfg:
                # can be used to replace attributes...
                src = cfg['where']
                tgt = cfg['set']
                if len(cfg['equals']) != len(cfg['to']):
                    raise KartographError(
                        'attributes: "equals" and "to" arrays must be of same length'
                    )
                for i in range(len(cfg['equals'])):
                    if feature.props[src] == cfg['equals'][i]:
                        node.setAttribute('data-' + tgt, cfg['to'][i])

        return node
Ejemplo n.º 5
0
    def project_geometry(self, geometry):
        """ converts the given geometry to the view coordinates """
        geometries = hasattr(geometry, 'geoms') and geometry.geoms or [
            geometry
        ]
        res = []

        # at first shift polygons
        #geometries = []
        #for geom in unshifted_geometries:
        #    geometries += self._shift_polygon(geom)

        for geom in geometries:
            if isinstance(geom, Polygon):
                res += self.project_polygon(geom)
            elif isinstance(geom, LineString):
                rings = self.project_linear_ring(geom)
                res += map(LineString, rings)
            elif isinstance(geom, Point):
                res.append(self.project((geom.x, geom.y)))
            else:
                raise KartographError('unknown geometry type %s' % geometry)

        if len(res) > 0:
            if isinstance(res[0], Polygon):
                if len(res) > 1:
                    return MultiPolygon(res)
                else:
                    return res[0]
            elif isinstance(res[0], LineString):
                if len(res) > 1:
                    return MultiLineString(res)
                else:
                    return LineString(res[0])
            else:
                if len(res) > 1:
                    return MultiPoint(res)
                else:
                    return Point(res[0])
Ejemplo n.º 6
0
def _get_label_position(geometry, pos):
    if pos == 'centroid' and not (geometry is None):
        pt = geometry.centroid
        return (pt.x, pt.y)
    else:
        raise KartographError('unknown label positioning mode ' + pos)