コード例 #1
0
    def get_2d_dataset(self, **kwargs):
        # TODO apply filtering.  Should start supporting CQL soon.
        layers, srs, bbox, width, height, styles, bgcolor, transparent, time, elevation, v, filter = [
            kwargs[k] if k in kwargs else None for k in [
                'layers', 'srs', 'bbox', 'width', 'height', 'styles',
                'bgcolor', 'transparent', 'time', 'elevation', 'v', 'filter'
            ]
        ]
        ds = self.dataset.Clone()
        minx, miny, maxx, maxy = bbox
        if filter is None:
            filter = {}

        if self.requires_time and not time:
            raise Exception("this service requires a time parameter")
        if self.requires_elevation and not elevation:
            raise Exception('this service requires an elevation')

        ss = None
        if type(self.styles) is dict:
            if not styles and 'default' in self.styles:
                ss = self.styles['default']
            elif styles:
                ss = self.styles[styles]
        else:
            ss = self.styles

        ctx = RenderingContext(ss, minx, miny, maxx, maxy, width, height)

        t_srs = osr.SpatialReference()
        t_srs.ImportFromEPSG(int(srs))
        l0 = self.dataset.GetLayer(0)
        s_srs = l0.GetSpatialRef()

        s_mins = Point(minx, miny, srid=t_srs.wkt)
        s_maxs = Point(maxx, maxy, srid=t_srs.wkt)
        s_mins.transform(s_srs.wkt)
        s_maxs.transform(s_srs.wkt)

        ls = {}
        crx = osr.CoordinateTransformation(s_srs, t_srs)

        def xform(f):
            f.SetGeometry(f.geometry().Transform(crx))

        for query_layer in layers:
            ls[query_layer] = ds.GetLayerByName(query_layer)
            ls[query_layer].ResetReading()
            ls[query_layer].SetSpatialFilterRect(s_mins.x, s_mins.y, s_maxs.x,
                                                 s_maxs.y)

        for query_layer in layers:
            mysrs = self.nativesrs(query_layer)
            if mysrs == srs:
                ctx.render(ls[query_layer], lambda k: k[query_layer])
            else:
                ctx.render(ls[query_layer], lambda k: xform(k[query_layer]))

        return ctx.surface
コード例 #2
0
    def get_2d_dataset(self, **kwargs):
        layers, srs, bbox, width, height, styles, bgcolor, transparent, time, elevation, v, filter = [
            kwargs[k] if k in kwargs else None for k in [
                'layers', 'srs', 'bbox', 'width', 'height', 'styles',
                'bgcolor', 'transparent', 'time', 'elevation', 'v', 'filter'
            ]
        ]
        minx, miny, maxx, maxy = bbox

        if filter is None:
            filter = {}

        if self.requires_time and not time:
            raise Exception("this service requires a time parameter")
        if self.requires_elevation and not elevation:
            raise Exception('this service requires an elevation')

        ss = None
        required_fields = tuple()
        if type(self.styles) is dict:
            if not styles and 'default' in self.styles:
                ss = self.styles['default']
            elif styles:
                ss = self.styles[styles]
        else:
            ss = self.styles
            required_fields = ss.required_fields

        ctx = RenderingContext(ss, minx, miny, maxx, maxy, width, height)

        t_srs = djgdal.SpatialReference(srs)
        s_srs = djgdal.SpatialReference(self.nativesrs(layers[0]))

        s_mins = Point(minx, miny, srid=t_srs.wkt)
        s_maxs = Point(maxx, maxy, srid=t_srs.wkt)
        s_mins.transform(s_srs.wkt)
        s_maxs.transform(s_srs.wkt)

        geom = GEOSGeometry(
            'POLYGON(({minx} {miny}, {maxx} {miny}, {maxx} {maxy}, {minx} {maxy}, {minx} {miny}))'
            .format(minx=s_mins.x, miny=s_mins.y, maxx=s_maxs.x,
                    maxy=s_maxs.y))

        for query_layer in layers:
            filter[query_layer + "__bboverlaps"] = geom

        def xform(g):
            if self.simplify:
                k = g.simplify((maxx - minx) / width)
                if k:
                    g = k
            g.transform(t_srs.wkt)
            return g

        for query_layer in layers:
            qs = self.cls.objects.filter(**filter)
            if required_fields:
                qs = qs.only(*required_fields).values(*required_fields)
            else:
                qs = qs.values()

            mysrs = self.nativesrs(query_layer)
            if mysrs == srs:
                ctx.render(qs, lambda k: k[query_layer])
            else:
                ctx.render(qs, lambda k: xform(k[query_layer]))

        return ctx.surface