def to_representation(self, instance): ret = super(LayerSerializer, self).to_representation(instance) qgs_maplayer = self.qgs_project.mapLayers()[instance.qgs_layer_id] group = instance.project.group # add attributes/fields ret['fields'] = self.get_attributes(instance) # add infoformat and infourl ret['infoformat'] = '' ret['infourl'] = '' lidname = instance.qgs_layer_id if instance.project.wms_use_layer_ids else instance.name # add bbox if instance.geometrytype != QGIS_LAYER_TYPE_NO_GEOM: if instance.extent: ret['bbox'] = instance.extent_rect else: # get from QgsMapLayer instance extent = qgs_maplayer.extent() ret['bbox'] = {} ret['bbox']['minx'] = extent.xMinimum() ret['bbox']['miny'] = extent.yMinimum() ret['bbox']['maxx'] = extent.xMaximum() ret['bbox']['maxy'] = extent.yMaximum() # add capabilities ret['capabilities'] = get_capabilities4layer(qgs_maplayer) # add styles # FIXME: restore in the future for styles map management #ret['styles'] = self.qgis_projectsettings_wms.layers[lidname]['styles'] ret['source'] = {'type': instance.layer_type} # add options for wms layer if instance.layer_type in [ Layer.TYPES.wms, Layer.TYPES.arcgismapserver ]: if instance.layer_type == Layer.TYPES.wms: datasourceWMS = QueryDict(instance.datasource) else: datasourceWMS = datasourcearcgis2dict(instance.datasource) if ('username' not in ret['source'] or 'password' not in ret['source']) and 'type=xyz' \ not in instance.datasource: # rebuild the dict for paramenters repeat n times i.e. 'layers' and 'styles' if isinstance(datasourceWMS, QueryDict): for p in datasourceWMS.lists(): if p[0] in ('layers', 'styles'): ret['source'].update({p[0]: ','.join(p[1])}) else: ret['source'].update({p[0]: datasourceWMS[p[0]]}) else: ret['source'].update(datasourceWMS) ret['source']['external'] = instance.external # replace crs property if is not none with dict structure if ret['crs']: crs = QgsCoordinateReferenceSystem(f'EPSG:{ret["crs"]}') ret['crs'] = { 'epsg': crs.postgisSrid(), 'proj4': crs.toProj4(), 'geographic': crs.isGeographic(), 'axisinverted': crs.hasAxisInverted() } # add metadata ret['metadata'] = self.get_metadata(instance, qgs_maplayer) # eval editor_form_structure if ret['editor_form_structure']: ret['editor_form_structure'] = eval(instance.editor_form_structure) # add ows ret['ows'] = self.get_ows(instance) return ret
def test_get_capabilities4layer(self): """ Test same name util func """ layer = self.project310.instance.layer_set.get( qgs_layer_id='countries_simpl20171228095706310') self.assertEqual(get_capabilities4layer(None, layer=layer), 3)
def to_representation(self, instance): ret = super(LayerSerializer, self).to_representation(instance) qgs_maplayer = self.qgs_project.mapLayers()[instance.qgs_layer_id] # add attributes/fields ret['fields'] = self.get_attributes(instance) # add infoformat and infourl ret['infoformat'] = '' ret['infourl'] = '' #lidname = instance.qgs_layer_id if instance.project.wms_use_layer_ids else instance.name # add bbox if instance.geometrytype != QGIS_LAYER_TYPE_NO_GEOM: if instance.extent: ret['bbox'] = instance.extent_rect else: # get from QgsMapLayer instance extent = qgs_maplayer.extent() ret['bbox'] = {} ret['bbox']['minx'] = extent.xMinimum() ret['bbox']['miny'] = extent.yMinimum() ret['bbox']['maxx'] = extent.xMaximum() ret['bbox']['maxy'] = extent.yMaximum() # add capabilities ret['capabilities'] = get_capabilities4layer(qgs_maplayer) # add styles # FIXME: restore in the future for styles map management #ret['styles'] = self.qgis_projectsettings_wms.layers[lidname]['styles'] ret['source'] = {'type': instance.layer_type} # add options for wms layer if instance.layer_type in [ Layer.TYPES.wms, Layer.TYPES.arcgismapserver ]: if instance.layer_type == Layer.TYPES.wms: datasource_wms = QueryDict(instance.datasource) else: datasource_wms = datasourcearcgis2dict(instance.datasource) if ('username' not in ret['source'] or 'password' not in ret['source']) and 'type=xyz' \ not in instance.datasource: # rebuild the dict for paramenters repeat n times i.e. 'layers' and 'styles' if isinstance(datasource_wms, QueryDict): for p in datasource_wms.lists(): if p[0] in ('layers', 'styles'): ret['source'].update({p[0]: ','.join(p[1])}) else: ret['source'].update({p[0]: datasource_wms[p[0]]}) else: ret['source'].update(datasource_wms) ret['source']['external'] = instance.external if instance.external and instance.layer_type == Layer.TYPES.wms: try: wms = WebMapService(ret['source']['url'], version='1.3.0') format_options = wms.getOperationByName( 'GetFeatureInfo').formatOptions if format_options: # Filter format by supported by G3W-CLIENT formats = list( set(format_options).intersection( set(settings.EXTERNAL_WMS_INFOFORMATS_SUPPORTED ))) if formats: ret['infoformat'] = formats[0] ret['infoformats'] = formats except Exception as e: logger.debug( f'WMS layer GetFeatureInfo formats available: {e}') # replace crs property if is not none with dict structure if ret['crs']: crs = QgsCoordinateReferenceSystem(f'EPSG:{ret["crs"]}') # Patch for Proj4 > 4.9.3 version if ret["crs"] == 3003: proj4 = "+proj=tmerc +lat_0=0 +lon_0=9 +k=0.9996 +x_0=1500000 +y_0=0 +ellps=intl " \ "+towgs84=-104.1,-49.1,-9.9,0.971,-2.917,0.714,-11.68 +units=m +no_defs" else: proj4 = crs.toProj4() ret['crs'] = { 'epsg': crs.postgisSrid(), 'proj4': proj4, 'geographic': crs.isGeographic(), 'axisinverted': crs.hasAxisInverted() } # add metadata ret['metadata'] = self.get_metadata(instance, qgs_maplayer) # eval editor_form_structure if ret['editor_form_structure']: ret['editor_form_structure'] = eval(instance.editor_form_structure) # add ows ret['ows'] = self.get_ows(instance) # For temporal properties if instance.temporal_properties: ret['qtimeseries'] = json.loads(instance.temporal_properties) if ret['qtimeseries'] and ret['qtimeseries'][ 'mode'] == 'FeatureDateTimeInstantFromField': # Add start_date end end_date: findex = qgs_maplayer.dataProvider().fieldNameIndex( ret['qtimeseries']['field']) ret['qtimeseries']['start_date'] = qgs_maplayer.minimumValue( findex) ret['qtimeseries']['end_date'] = qgs_maplayer.maximumValue( findex) if isinstance(ret['qtimeseries']['start_date'], QDate) or isinstance( ret['qtimeseries']['start_date'], QDateTime): if not hasattr(QDate, 'isoformat'): QDate.isoformat = lambda d: d.toString(Qt.ISODate) if not hasattr(QDateTime, 'isoformat'): QDateTime.isoformat = lambda d: d.toString( Qt.ISODateWithMs) ret['qtimeseries']['start_date'] = ret['qtimeseries'][ 'start_date'].isoformat() ret['qtimeseries']['end_date'] = ret['qtimeseries'][ 'end_date'].isoformat() return ret