def __init__(self, package_id, resource_id, workspace_name, layer_name, layer_version, username, geoserver, store=None, workspace=None, lat_field=None, lng_field=None): self.geoserver = Geoserver.from_ckan_config() self.name = layer_name self.layer_version = layer_version self.username = username self.file_resource = toolkit.get_action("resource_show")(None, {"id": resource_id}) self.package_id = package_id self.resource_id = resource_id self.store = self.geoserver.get_datastore(workspace, store, workspace_name, layer_version) self.workspace_name = workspace_name url = self.file_resource["url"] kwargs = {"resource_id": self.file_resource["id"]} # Determine whether to handle the data with shapefile or datastored csv operators if url.endswith('.zip'): cls = Shapefile elif url.endswith('.csv'): cls = Datastored kwargs.update({ "lat_field": lat_field, "lng_field": lng_field }) else: # The resource cannot be spatialized raise Exception(toolkit._("Only CSV and Shapefile data can be spatialized")) # '**' unpacks the kwargs dictionary which can contain an arbitrary number of arguments self.data = cls(**kwargs) # Spatialize if not self.data.publish(): # Spatialization failed raise Exception(toolkit._("Spatialization failed."))
def unpublish_ogc(context, data_dict): """ Un-publishes the Geoserver layer based on the resource identifier. Retrieves the Geoserver layer name and package identifier to construct layer and remove it. """ resource_id = data_dict.get("resource_id") layer_name = data_dict.get("layer_name") layer_name = "NGDS:" + resource_id username = context.get('user') file_resource = toolkit.get_action("resource_show")(None, {"id": resource_id}) geoserver = Geoserver.from_ckan_config() package_id = model.Resource.get(resource_id).resource_group.package_id def unpub(): layer = Layer(geoserver=geoserver, layer_name=layer_name, resource_id=resource_id, package_id=package_id, username=username) return layer try: layer = unpub() except socket.error: h.flash_error( _("Error connecting to geoserver. Please contact the site administrator if this problem persists.")) return False layer.remove() h.flash_success( _("This resource has successfully been unpublished.")) return True
def __init__(self, package_id, resource_id, workspace_name, layer_name, layer_version, username, geoserver, store=None, workspace=None, lat_field=None, lng_field=None, join_key=None): self.geoserver = Geoserver.from_ckan_config() self.name = layer_name self.layer_version = layer_version self.username = username if resource_id.endswith("_multi"): self.file_resource = toolkit.get_action("package_show")(None, { "id": package_id }) else: self.file_resource = toolkit.get_action("resource_show")(None, { "id": resource_id }) self.package_id = package_id self.resource_id = resource_id self.store = self.geoserver.get_datastore(workspace, store, workspace_name, layer_version) self.workspace_name = workspace_name self.join_key = join_key if not resource_id.endswith("_multi"): url_ = self.file_resource["url"] kwargs = { "resource_id": self.file_resource["id"] } # Determine whether to handle the data with shapefile or datastored csv operators if url_.endswith('.zip'): cls = Shapefile elif url_.endswith('.csv'): cls = Datastored kwargs.update({ "lat_field": lat_field, "lng_field": lng_field }) elif url_.endswith('.tif'): cls = RasterFile else: # The resource cannot be spatialized raise Exception(toolkit._("Only CSV and Shapefile data can be spatialized")) else: kwargs = { "package_id": self.package_id } if resource_id == 'schema_descriptor_multi': cls = MultiDatastored kwargs.update({ "lat_field": lat_field, "lng_field": lng_field, "join_key": self.join_key }) elif resource_id == 'shapefile_multi': cls = MultiShapeFile elif resource_id == 'schema_descriptor_timeseries_multi': cls = MultiRasterFile else: # The resource cannot be spatialized raise Exception(toolkit._("Can not spatialize package.")) # '**' unpacks the kwargs dictionary which can contain an arbitrary number of arguments self.data = cls(**kwargs)
def __init__(self, package_id, resource_id, workspace_name, layer_name, layer_version, username, geoserver, store=None, workspace=None, lat_field=None, lng_field=None): self.geoserver = Geoserver.from_ckan_config() self.name = layer_name self.layer_version = layer_version self.username = username self.file_resource = toolkit.get_action("resource_show")( None, { "id": resource_id }) self.package_id = package_id self.resource_id = resource_id self.store = self.geoserver.get_datastore(workspace, store, workspace_name, layer_version) self.workspace_name = workspace_name url = self.file_resource["url"] kwargs = {"resource_id": self.file_resource["id"]} # Determine whether to handle the data with shapefile or datastored csv operators if url.endswith('.zip'): cls = Shapefile elif url.endswith('.csv'): cls = Datastored kwargs.update({"lat_field": lat_field, "lng_field": lng_field}) else: # The resource cannot be spatialized raise Exception( toolkit._("Only CSV and Shapefile data can be spatialized")) # '**' unpacks the kwargs dictionary which can contain an arbitrary number of arguments self.data = cls(**kwargs) # Spatialize if not self.data.publish(): # Spatialization failed raise Exception(toolkit._("Spatialization failed."))
def unpublish_ogc(context, data_dict): """ Un-publishes the Geoserver layer based on the resource identifier. Retrieves the Geoserver layer name and package identifier to construct layer and remove it. """ resource_id = data_dict.get("resource_id") layer_name = data_dict.get("layer_name") layer_name = "NGDS:" + resource_id username = context.get('user') api_call_type = data_dict.get("api_call_type", "ui") file_resource = toolkit.get_action("resource_show")(None, { "id": resource_id }) geoserver = Geoserver.from_ckan_config() package_id = model.Resource.get(resource_id).resource_group.package_id def unpub(): layer = Layer(geoserver=geoserver, layer_name=layer_name, resource_id=resource_id, package_id=package_id, username=username) return layer try: layer = unpub() except socket.error: log.debug( "Error connecting to geoserver. Please contact the site administrator if this problem persists." ) if api_call_type == 'ui': h.flash_error( _("Error connecting to geoserver. Please contact the site administrator if this problem persists." )) return False layer.remove() log.debug("This resource has successfully been unpublished.") if api_call_type == 'ui': h.flash_success(_("This resource has successfully been unpublished.")) return True
def unpublish_ogc(context, data_dict): """ Un-publishes the Geoserver layer based on the resource identifier. Retrieves the Geoserver layer name and package identifier to construct layer and remove it. """ # check if the user is allowed to edit/publish this package check_data_dict = { 'id': data_dict.get("package_id", None) } logic.check_access('package_update', context, check_data_dict) resource_id = data_dict.get("resource_id", None) layer_name = data_dict.get("layer_name", None) username = context.get('user') api_call_type = data_dict.get("api_call_type", "ui") package_id = data_dict.get("package_id", None) descriptor_name = config.get('geoserver.descriptor_name', 'schema_descriptor') pkg = toolkit.get_action('package_show')(None, { 'id': package_id }) extras = pkg.get('extras', []) for extra in extras: key = extra.get('key', None) if key == descriptor_name: schema_descriptor = json.loads(extra.get('value')) # if api call if data_dict.get("package_id", None) is not None and len(data_dict) == 1: if schema_descriptor: # MultiShapefile if helpers.shapefile_publishing_requirements_fulfiled(data_dict.get("package_id", None)): resource_id = "shapefile_multi" layer_name = "shapefile_multi" # MultiRasterfile elif helpers.geoserver_rasters_to_publish(data_dict.get("package_id", None)): resource_id = "schema_descriptor_timeseries_multi" layer_name = "schema_descriptor_timeseries_multi" # MultiDatastore else: for member in schema_descriptor.get("members"): # if single if member.get('resource_type') == 'observations_with_geometry': resource_id = member.get('resource_name')[0] layer_name = member.get('resource_name')[0] # if multi get lat/lng from correct file if member.get('resource_type') == 'observed_geometries': resource_id = "schema_descriptor_multi" layer_name = "schema_descriptor_multi" # if resource_id.endswith("_multi"): # file_resource = toolkit.get_action("package_show")(None, { # "id": package_id # }) # else: # file_resource = toolkit.get_action("resource_show")(None, { # "id": resource_id # }) geoserver = Geoserver.from_ckan_config() def unpub(): layer = Layer.unpublish(geoserver, layer_name, resource_id, package_id, username) return layer try: layer = unpub() except socket.error: log.debug("Error connecting to geoserver. Please contact the site administrator if this problem persists.") if api_call_type == 'ui': h.flash_error(_("Error connecting to geoserver. Please contact the site administrator if this problem persists.")) return False if data_dict.get("package_id", None) is not None and len(data_dict) <= 2: helpers.update_package_published_status(data_dict.get("package_id", None), False) log.debug("This resource has successfully been unpublished.") if api_call_type == 'ui': h.flash_success(_("This resource has successfully been unpublished.")) return { "success": True, "message": _("This resource has successfully been unpublished.") }
def unpublish_ogc(context, data_dict): """ Un-publishes the Geoserver layer based on the resource identifier. Retrieves the Geoserver layer name and package identifier to construct layer and remove it. """ # check if the user is allowed to edit/publish this package check_data_dict = {'id': data_dict.get("package_id", None)} logic.check_access('package_update', context, check_data_dict) resource_id = data_dict.get("resource_id", None) layer_name = data_dict.get("layer_name", None) username = context.get('user') api_call_type = data_dict.get("api_call_type", "ui") package_id = data_dict.get("package_id", None) descriptor_name = config.get('geoserver.descriptor_name', 'schema_descriptor') pkg = toolkit.get_action('package_show')(None, {'id': package_id}) extras = pkg.get('extras', []) for extra in extras: key = extra.get('key', None) if key == descriptor_name: schema_descriptor = json.loads(extra.get('value')) # if api call if data_dict.get("package_id", None) is not None and len(data_dict) == 1: if schema_descriptor: # MultiShapefile if helpers.shapefile_publishing_requirements_fulfiled( data_dict.get("package_id", None)): resource_id = "shapefile_multi" layer_name = "shapefile_multi" # MultiRasterfile elif helpers.geoserver_rasters_to_publish( data_dict.get("package_id", None)): resource_id = "schema_descriptor_timeseries_multi" layer_name = "schema_descriptor_timeseries_multi" # MultiDatastore else: for member in schema_descriptor.get("members"): # if single if member.get( 'resource_type') == 'observations_with_geometry': resource_id = member.get('resource_name')[0] layer_name = member.get('resource_name')[0] # if multi get lat/lng from correct file if member.get('resource_type') == 'observed_geometries': resource_id = "schema_descriptor_multi" layer_name = "schema_descriptor_multi" # if resource_id.endswith("_multi"): # file_resource = toolkit.get_action("package_show")(None, { # "id": package_id # }) # else: # file_resource = toolkit.get_action("resource_show")(None, { # "id": resource_id # }) geoserver = Geoserver.from_ckan_config() def unpub(): layer = Layer.unpublish(geoserver, layer_name, resource_id, package_id, username) return layer try: layer = unpub() except socket.error: log.debug( "Error connecting to geoserver. Please contact the site administrator if this problem persists." ) if api_call_type == 'ui': h.flash_error( _("Error connecting to geoserver. Please contact the site administrator if this problem persists." )) return False if data_dict.get("package_id", None) is not None and len(data_dict) <= 2: helpers.update_package_published_status( data_dict.get("package_id", None), False) log.debug("This resource has successfully been unpublished.") if api_call_type == 'ui': h.flash_success(_("This resource has successfully been unpublished.")) return { "success": True, "message": _("This resource has successfully been unpublished.") }