def get_table_details(self, filename, tablename): # return table name, id column name, geometry column name, and extent. mddata = DatabaseManager.get_metadata(filename) if mddata: layer_info = mddata.get(tablename, None) if layer_info: return (layer_info.get('table', None), layer_info.get('id_column', None), layer_info.get('geometry_column', None), layer_info.get('base_extent', None)) return (None, None, None, None)
def get_table_details(self, filename, tablename): # return table name, id column name, geometry column name, and extent. mddata = DatabaseManager.get_metadata(filename) if mddata: layer_info = mddata.get(tablename, None) if layer_info: return (layer_info.get('table', None), layer_info.get('id_column', None), layer_info.get('geometry_column', None), layer_info.get('base_extent', None)) return (None, None, None, None)
def add_layer_obj(self, map): """ Create mapserver layer object. The raster data for the layer is located at filename, which should be an absolute path on the local filesystem. """ # inspect data if we haven't yet self._inspect_data() # create a layer object layer = mapscript.layerObj() # NAME layer.name = "DEFAULT" # TODO: filename? # TYPE layer.type = mapscript.MS_LAYER_POLYGON # STATUS layer.status = mapscript.MS_ON # mark layer as queryable layer.template = "query" # anything non null and with length > 0 works here # Extract the base table and attribute table from typeNames # typeNames = {base_filename}-{base tanlename}.{attribute_filename}-{attribute tablename} # i.e. SH_Network.gdb-ahgfcatchment.stream_attributesv1.1.5.gdb-climate_lut type_name = self.request.params.get('typeNames', None) if type_name is None: raise Exception("Missing typeNames") # Parse to get filenames and layer names so that we can get the corresponding geometry and attribute tables. base_fname, base_table, attr_fname, attr_table = self.parse_typeName( type_name) if attr_fname is None or attr_table is None: raise Exception( "Invalid typeNames in request: no such layer '{layer}'".format( layer=attr_table)) # set layer name layer.name = type_name # get the attribute table and its metadata db_attr_table, id_col, geom_col, extent = self.get_table_details( attr_fname, attr_table) if db_attr_table is None: raise Exception( "Invalid typeNames in request: no such layer '{layer}'".format( layer=attr_table)) # Get the corresposning base table, and its id and geometry column names db_base_table = None common_col = None if base_table: db_base_table, id_col, geom_col, extent = self.get_table_details( base_fname, base_table) if db_base_table is None or id_col is None or geom_col is None: raise Exception( "Missing or invalid base table for {layer}".format( layer=base_table)) # Get the foreign key i.e. the joinable column common_col = self.request.params.get('foreignKey', None) if common_col is None: raise Exception("Missing foreignKey in request") else: db_base_table = db_attr_table if extent: layer.setExtent(extent['xmin'], extent['ymin'], extent['xmax'], extent['ymax']) # get the feature ids. It must be in the format: {layername}.fid featureId = self.request.params.get('featureID', '') fidlist = featureId.split(',') fid = ','.join([ v.split('{layername}.'.format(layername=layer.name))[1] for v in fidlist if len(v.split('.')) > 1 ]) if len(fid) == 0: fid = None # get the attribute names. attributeNames = self.request.params.get('attributes', None) # DATA, in the format of "<column> from <tablename> using unique fid using srid=xxxx" # table must have fid and geom # table_attribute in the form 'table-name:attrubute1' if DatabaseManager.is_configured(): # Connection to POSTGIS DB server layer.connectiontype = mapscript.MS_POSTGIS layer.connection = DatabaseManager.connection_details() if db_attr_table != db_base_table: # Get all attributes of the attribute table if not specified layer_info = None if attributeNames is None: mddata = DatabaseManager.get_metadata(attr_fname) if mddata: layer_info = mddata.get(attr_table, None) if layer_info: fields = [i.lower() for i in layer_info.get('fields')] # Remove the id column and foreign key a_idcol = layer_info.get('id_column') for item in [a_idcol.lower(), common_col.lower()]: if item in fields: fields.remove(item) attributeNames = ','.join(fields) if fid: newtable = "(select b.*, {attributes} from {atable} a join {base} b on a.{ccol} = b.{ccol} and b.{idcol} in ({ids}))".format( attributes=attributeNames, atable=db_attr_table, base=db_base_table, ccol=common_col, idcol=id_col, ids=fid, geom=geom_col) else: newtable = "(select b.*, {attributes} from {atable} a join {base} b on a.{ccol} = b.{ccol})".format( attributes=attributeNames, atable=db_attr_table, base=db_base_table, ccol=common_col, idcol=id_col, geom=geom_col) else: if fid: newtable = "(select * from {base} where {idcol} in ({ids}))".format( base=db_base_table, idcol=id_col, ids=fid) else: newtable = "(select * from {base})".format( base=db_base_table) srid = self.request.params.get('SRID', '4326') layer.data = "{geom} from {table} as new_layer using unique {idcol} using srid={srid}".format( geom=geom_col, table=newtable, idcol=id_col, srid=srid) # Defer closing connection layer.addProcessing("CLOSE_CONNECTION=DEFER") else: # TO DO: read from file raise Exception("Database is not configured.") # PROJECTION ... should we set this properly? crs = self._data['crs'] layer.setProjection("init={}".format(crs)) # METADATA # TODO: check return value of setMetaData MS_SUCCESS/MS_FAILURE layer.setMetaData("gml_types", "auto") layer.setMetaData("gml_featureid", id_col) # Shall be the id column of the base table layer.setMetaData("gml_include_items", "all") # allow raster queries layer.setMetaData("wfs_include_items", "all") layer.setMetaData("wfs_srs", "EPSG:4326 EPSG:3857") # projection to serve layer.setMetaData("wfs_title", "BCCVL Layer") # title required for GetCapabilities # TODO: metadata # other things like title, author, attribution etc... return map.insertLayer(layer)
def add_layer_obj(self, map): """ Create mapserver layer object. The raster data for the layer is located at filename, which should be an absolute path on the local filesystem. """ # inspect data if we haven't yet self._inspect_data() # create a layer object layer = mapscript.layerObj() # NAME layer.name = "DEFAULT" # TODO: filename? # TYPE layer.type = mapscript.MS_LAYER_POLYGON # STATUS layer.status = mapscript.MS_ON # mark layer as queryable layer.template = "query" # anything non null and with length > 0 works here # Extract the base table and attribute table from typeNames # typeNames = {base_filename}-{base tanlename}.{attribute_filename}-{attribute tablename} # i.e. SH_Network.gdb-ahgfcatchment.stream_attributesv1.1.5.gdb-climate_lut type_name = self.request.params.get("typeNames", None) if type_name is None: raise Exception("Missing typeNames") # Parse to get filenames and layer names so that we can get the corresponding geometry and attribute tables. base_fname, base_table, attr_fname, attr_table = self.parse_typeName(type_name) if attr_fname is None or attr_table is None: raise Exception("Invalid typeNames in request: no such layer '{layer}'".format(layer=attr_table)) # set layer name layer.name = type_name # get the attribute table and its metadata db_attr_table, id_col, geom_col, extent = self.get_table_details(attr_fname, attr_table) if db_attr_table is None: raise Exception("Invalid typeNames in request: no such layer '{layer}'".format(layer=attr_table)) # Get the corresposning base table, and its id and geometry column names db_base_table = None common_col = None if base_table: db_base_table, id_col, geom_col, extent = self.get_table_details(base_fname, base_table) if db_base_table is None or id_col is None or geom_col is None: raise Exception("Missing or invalid base table for {layer}".format(layer=base_table)) # Get the foreign key i.e. the joinable column common_col = self.request.params.get("foreignKey", None) if common_col is None: raise Exception("Missing foreignKey in request") else: db_base_table = db_attr_table if extent: layer.setExtent(extent["xmin"], extent["ymin"], extent["xmax"], extent["ymax"]) # get the feature ids. It must be in the format: {layername}.fid featureId = self.request.params.get("featureID", "") fidlist = featureId.split(",") fid = ",".join( [v.split("{layername}.".format(layername=layer.name))[1] for v in fidlist if len(v.split(".")) > 1] ) if len(fid) == 0: fid = None # get the attribute names. attributeNames = self.request.params.get("attributes", None) # DATA, in the format of "<column> from <tablename> using unique fid using srid=xxxx" # table must have fid and geom # table_attribute in the form 'table-name:attrubute1' if DatabaseManager.is_configured(): # Connection to POSTGIS DB server layer.connectiontype = mapscript.MS_POSTGIS layer.connection = DatabaseManager.connection_details() if db_attr_table != db_base_table: # Get all attributes of the attribute table if not specified layer_info = None if attributeNames is None: mddata = DatabaseManager.get_metadata(attr_fname) if mddata: layer_info = mddata.get(attr_table, None) if layer_info: fields = [i.lower() for i in layer_info.get("fields")] # Remove the id column and foreign key a_idcol = layer_info.get("id_column") for item in [a_idcol.lower(), common_col.lower()]: if item in fields: fields.remove(item) attributeNames = ",".join(fields) if fid: newtable = "(select b.*, {attributes} from {atable} a join {base} b on a.{ccol} = b.{ccol} and b.{idcol} in ({ids}))".format( attributes=attributeNames, atable=db_attr_table, base=db_base_table, ccol=common_col, idcol=id_col, ids=fid, geom=geom_col, ) else: newtable = "(select b.*, {attributes} from {atable} a join {base} b on a.{ccol} = b.{ccol})".format( attributes=attributeNames, atable=db_attr_table, base=db_base_table, ccol=common_col, idcol=id_col, geom=geom_col, ) else: if fid: newtable = "(select * from {base} where {idcol} in ({ids}))".format( base=db_base_table, idcol=id_col, ids=fid ) else: newtable = "(select * from {base})".format(base=db_base_table) srid = self.request.params.get("SRID", "4326") layer.data = "{geom} from {table} as new_layer using unique {idcol} using srid={srid}".format( geom=geom_col, table=newtable, idcol=id_col, srid=srid ) # Defer closing connection layer.addProcessing("CLOSE_CONNECTION=DEFER") else: # TO DO: read from file raise Exception("Database is not configured.") # PROJECTION ... should we set this properly? crs = self._data["crs"] layer.setProjection("init={}".format(crs)) # METADATA # TODO: check return value of setMetaData MS_SUCCESS/MS_FAILURE layer.setMetaData("gml_types", "auto") layer.setMetaData("gml_featureid", id_col) # Shall be the id column of the base table layer.setMetaData("gml_include_items", "all") # allow raster queries layer.setMetaData("wfs_include_items", "all") layer.setMetaData("wfs_srs", "EPSG:4326 EPSG:3857") # projection to serve layer.setMetaData("wfs_title", "BCCVL Layer") # title required for GetCapabilities # TODO: metadata # other things like title, author, attribution etc... return map.insertLayer(layer)