예제 #1
0
파일: forest.py 프로젝트: silpol/tryton-bef
    def situation_map_gen(cls, records):
        """Render the situation map"""
        CadPlot = Pool().get('cadastre.parcelle')
        Plot = Pool().get('forest.plot')
        for record in records:
            cad_plots = [plot.geom for plot in record.cad_plots]
            cad_plots, envelope, cad_area = get_as_epsg4326(cad_plots)
            plots = [plot.geom for plot in record.plots]
            plots, plot_bbox, _plots_area = get_as_epsg4326(plots)

            if envelope is None:
                continue

            # Compute the envelope
            if plot_bbox is not None:
                envelope = envelope_union(envelope, plot_bbox)

            # Include the geometry of the town in the bbox of the map
            if record.address is not None and record.address.my_city is not None:
                # Include the town from the address in the bbox
                town_geo = osr_geo_from_field(record.address.my_city.contour)

                dst = osr.SpatialReference()
                dst.SetWellKnownGeogCS("EPSG:4326")
                town_geo.TransformTo(dst)
                envelope = envelope_union(envelope, town_geo.GetEnvelope())

            # Map title
            title = u'Plan de situation\n'
            title += u'Propriétaire: %s\n' % record.owner.name
            if record.address is not None \
                    and record.address.city is not None \
                    and record.address.my_city is not None:
                city = record.address.city
                dep = record.address.my_city.subdivision.parent.code.split('-')[1]
                title += u'Commune: %s (%s)\n' % (city, dep)
            title += u'Surface: %02i ha %02i a %02i ca\n\nLe ' % cls._area_to_a(cad_area)
            title += date.today().strftime('%02d/%02m/%Y')

            m = MapRender(1024, 768, envelope, True)
            m.add_bg()
            for plot in cad_plots:
                m.plot_geom(plot, None, u'Bois de La Forêt', color=CadPlot.COLOR, bgcolor=CadPlot.BGCOLOR)
            for plot in plots:
                m.plot_geom(plot, None, u'Parcelle forestière', linestyle='--', color=Plot.COLOR, bgcolor=Plot.BGCOLOR)

            data_nl = m.render()
            m.plot_legend()
            m.plot_compass()
            m.plot_scaling()
            cls._plot_logo(m)
            m.plot_title(title)
            data = m.render()
            cls.write([record], {
                'situation_map': buffer(data),
                'situation_map_nl': buffer(data_nl),
            })
예제 #2
0
파일: site.py 프로젝트: silpol/tryton-bef
    def situation_map_gen(cls, records):
        """Render the situation map"""        
        for record in records:
            # Récupère l'étendu de la zone de travaux
            areas, _envelope, _area = get_as_epsg4326([record.geom])
            
            # Léger dézoom pour afficher correctement les points qui touchent la bbox
            envelope = [
                _envelope[0] - 0.001,
                _envelope[1] + 0.001,
                _envelope[2] - 0.001,
                _envelope[3] + 0.001,
            ]  

            if envelope is None:
                continue
                   
            # Include the geometry of the town in the bbox of the map
            if record.commune is not None and record.commune.name is not None:
                # Include the town from the commune in the bbox
                town_geo = osr_geo_from_field(record.commune.contour)

                dst = osr.SpatialReference()
                dst.SetWellKnownGeogCS("EPSG:4326")
                town_geo.TransformTo(dst)
                envelope = envelope_union(envelope, town_geo.GetEnvelope())

            # Map title
            title = u'Plan de situation communal\n'
            title += u'Propriétaire: %s\n' % record.owner.name
            if record.commune is not None \
                    and record.commune.name is not None \
                    and record.commune.name is not None:
                city = record.commune.name
                dep = record.commune.subdivision.parent.code.split('-')[1]
                title += u'Commune: %s (%s)\n' % (city, dep)
            title += u'Surface: %02i ha %02i a %02i ca\n\nLe ' % cls._area_to_a(_area)
            title += date.today().strftime('%02d/%02m/%Y')

            m = MapRender(1024, 768, envelope, True)
            # Ajoute le fond de carte
            m.add_bg()                     

            # Ajoute la zone de chantier
            m.plot_geom(areas[0], None, u'Site', color=cls.COLOR, bgcolor=cls.BGCOLOR) 

            data_nl = m.render()
            m.plot_legend()
            m.plot_compass()
            m.plot_scaling()
            cls._plot_logo(m)
            m.plot_title(title)
            data = m.render()
            cls.write([record], {
                'situation_map': buffer(data),
            })
예제 #3
0
파일: qgis.py 프로젝트: silpol/tryton-bef
    def get_layer(cls, records, model_name):
        """Return parameters for the model @model_name@.
        If some records of the model are not contained in @records@,
        then those record are returned in a separate layer that will is displayed
        as "Unselected" values.
        """
        # Look for a geometric field
        Field = Pool().get('ir.model.field')
        Model = Pool().get('ir.model')
        RecordModel = Pool().get(model_name)

        geo_field_name = ''
        geo_field = None
        m2o_fields = []
        for attr_name in dir(RecordModel):
            if attr_name in TRYTON_FIELDS:
                continue
            attr = getattr(RecordModel, attr_name)
            if isinstance(attr, fields.Many2One):
                m2o_fields.append(attr)
            if isinstance(attr, fields.Geometry):
                geo_field = attr
                geo_field_name = attr_name

        bbox = None
        unselected = set()
        srid = getattr(RecordModel, geo_field_name).srid

        for record in records:
            geo_value = getattr(record, geo_field_name)
            if geo_value is not None:
                geo = osr_geo_from_field(geo_value)
                bbox = envelope_union(geo.GetEnvelope(), bbox)

        # Look for unselected related records
        for m2o_field in m2o_fields:
            # Find the corresponding one2many field in the parent
            parent_model = Model.search([('model', '=', m2o_field.model_name)], limit=1)[0]
            Parent = Pool().get(m2o_field.model_name)
            o2mfield = Field.search([('ttype', '=', 'one2many'),
                                     ('model', '=', parent_model.id)])

            for o2m in o2mfield:
                parent_field = getattr(Parent, o2m.name)
                if parent_field.model_name == model_name and m2o_field.name == parent_field.field:
                    break
            else:
                continue

            for record in records:
                parent_record = getattr(record, m2o_field.name)
                for child in getattr(parent_record, parent_field.name):
                    if child in records:
                        continue
                    unselected.add(child.id)
            break

        layers = []
        layers.append({
            'model': model_name,
            'title': cls.TITLES.get(model_name, RecordModel.__doc__),
            'layerid': 'tryton_%s' % model_name.replace('.', '_'),
            'geo': True,
            'srid': srid,
            'filter': get_wfs_id_filter(sorted([record.id for record in records])),
            'color': cls._qgis_color(RecordModel.BGCOLOR),
            'color_border': cls._qgis_color(RecordModel.COLOR),
            'edittypes': {'id': {'type': QGIS_WIDGET_HIDDEN}},
            'geo_type': TRYTON_TO_QGIS[geo_field._type],
            'unfolded': 'true',
            'aliases': cls._get_aliases(model_name),
        })

        if len(unselected) != 0:
            layers.append({
                'model': model_name,
                'title': cls.TITLES.get(model_name, RecordModel.__doc__) + ' (unselected)',
                'layerid': 'tryton_unselected_%s' % model_name.replace('.', '_'),
                'geo': True,
                'srid': srid,
                'filter': get_wfs_id_filter(sorted(unselected)),
                'color': '255,255,255,0',
                'color_border': cls._qgis_color(RecordModel.BGCOLOR),
                'edittypes': {'id': {'type': QGIS_WIDGET_HIDDEN}},
                'geo_type': TRYTON_TO_QGIS[geo_field._type],
                'unfolded': 'false',
                'aliases': cls._get_aliases(model_name),
            })
        base_layers = layers

        deps_models = set()
        Record = Pool().get(model_name)
        symbols = []
        # Add dependency tables
        for m2o_field in m2o_fields:
            # Find the corresponding one2many field in the parent
            other_model_name = getattr(Record, m2o_field.name).model_name
            OtherModel = Pool().get(other_model_name)
            other_model = Model.search([('model', '=', other_model_name)], limit=1)[0]

            if OtherModel in deps_models:
                continue
            deps_models.add(OtherModel)

            layer_id = 'tryton_%s' % other_model.model.replace('.', '_')
            layers.append({
                'model': other_model.model,
                'title': cls.TITLES.get(other_model.model, other_model.name),
                'layerid': layer_id,
                'geo': False,
                'filter': None,
                'edittypes': {'id': {'type': QGIS_WIDGET_HIDDEN}},
                'unfolded': 'false',
                'aliases': cls._get_aliases(other_model.model),
            })

            for layer in base_layers:
                layer['edittypes'][m2o_field.name] = {
                    'type': QGIS_WIDGET_RELATIONAL,
                    'layer': layer_id,
                    'value': 'name',
                }

            if symbols == [] and (parent_model is None or other_model_name != parent_model.model):
                symbol_recs = Record.search([('id', 'in', list(unselected) + [record.id for record in records])])
                symbol_recs = set([getattr(symbol_rec, m2o_field.name) for symbol_rec in symbol_recs])
                symbol_recs -= set([None])

                for no, symbol in enumerate(symbol_recs):
                    color = no * 255 / len(symbol_recs)
                    symbols.append({
                        'no': no,
                        'value': symbol.id,
                        'label': symbol.rec_name,
                        'color': '%i,%i,255,255' % (color, color),
                        'color_border': '0,0,0,0',
                    })

                if geo_field._type in ('linestring', 'multilinestring'):
                    none_color = '0,0,0,255'
                else:
                    none_color = '255,255,255,255'

                symbols.append({
                    'no': len(symbol_recs),
                    'value': '',
                    'label': '(None)',
                    'color': none_color,
                    'color_border': '0,0,0,255',
                })

                base_layers[0].update({
                    'symbols': symbols,
                    'symbols_attr': m2o_field.name,
                })

                cls._add_symbol_style(base_layers[0], geo_field)

                if len(base_layers) > 1:
                    symbols = deepcopy(symbols)
                    # Set the border color to black for selected geometries
                    for symbol in symbols:
                        symbol['color_border'] = '0,0,0,255'

                    base_layers[1].update({
                        'symbols': symbols,
                        'symbols_attr': m2o_field.name,
                    })
                    cls._add_symbol_style(base_layers[1], geo_field)

        return layers, bbox, srid