Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if not srs:
raise GeoNodeException('Invalid Projection. Layer is missing CRS!')
srs.identify_epsg()
except SRSException:
pass
epsg_code = srs.srid
# can't find epsg code, then check if bbox is within the 4326 boundary
if epsg_code is None and (x_min <= bbox_x0 <= x_max and
x_min <= bbox_x1 <= x_max and
y_min <= bbox_y0 <= y_max and
y_min <= bbox_y1 <= y_max):
# set default epsg code
epsg_code = '4326'
elif epsg_code is None:
# otherwise, stop the upload process
raise GeoNodeException(
"Invalid Layers. "
"Needs an authoritative SRID in its CRS to be accepted")
# eliminate default EPSG srid as it will be added when this function returned
srid = epsg_code if epsg_code else '4326'
elif is_raster(filename):
gtif = gdal.Open(filename)
gt = gtif.GetGeoTransform()
prj = gtif.GetProjection()
srs = osr.SpatialReference(wkt=prj)
cols = gtif.RasterXSize
rows = gtif.RasterYSize
ext = []
xarr = [0, cols]
yarr = [0, rows]
except geoserver.catalog.FailedRequestError:
pass # There is no store, ergo the road is clear
else:
if store:
resources = store.get_resources()
if len(resources) == 0:
if overwrite:
logger.debug("Deleting previously existing store")
store.delete()
else:
raise GeoNodeException("Layer already exists")
else:
for resource in resources:
if resource.name == store_name:
if not overwrite:
raise GeoNodeException(
"Name already in use and overwrite is False")
existing_type = resource.resource_type
if existing_type != layer_type:
msg = ("Type of uploaded file {} ({}) does not "
"match type of existing resource type "
"{}".format(store_name, layer_type,
existing_type))
logger.error(msg)
raise GeoNodeException(msg)
def get_valid_layer_name(layer, overwrite):
"""Checks if the layer is a string and fetches it from the database.
"""
# The first thing we do is get the layer name string
if isinstance(layer, Layer):
layer_name = layer.name
elif isinstance(layer, basestring):
layer_name = layer
else:
msg = ('You must pass either a filename or a GeoNode layer object')
raise GeoNodeException(msg)
if overwrite:
return layer_name
else:
return get_valid_name(layer_name)
"""
native_name = name
cat = gs_catalog
# get workspace and store
workspace = cat.get_default_workspace()
# get (or create the datastore)
datastore = get_or_create_datastore(cat, workspace)
# check if datastore is of PostGIS type
if datastore.type != 'PostGIS':
msg = ("To use the createlayer application you must use PostGIS")
logger.error(msg)
raise GeoNodeException(msg)
# check if layer is existing
resources = datastore.get_resources()
for resource in resources:
if resource.name == name:
msg = "There is already a layer named %s in %s" % (name, workspace)
logger.error(msg)
raise GeoNodeException(msg)
attributes = get_attributes(geometry_type, attributes)
attributes_block = ""
for spec in attributes:
att_name, binding, opts = spec
nillable = opts.get("nillable", False)
attributes_block += (""
"{name}"
def cleanup(name, uuid):
"""Deletes GeoServer and Catalogue records for a given name.
Useful to clean the mess when something goes terribly wrong.
It also verifies if the Django record existed, in which case
it performs no action.
"""
try:
Layer.objects.get(name=name)
except Layer.DoesNotExist as e:
pass
else:
msg = ('Not doing any cleanup because the layer %s exists in the '
'Django db.' % name)
raise GeoNodeException(msg)
cat = gs_catalog
gs_store = None
gs_layer = None
gs_resource = None
# FIXME: Could this lead to someone deleting for example a postgis db
# with the same name of the uploaded file?.
try:
gs_store = cat.get_store(name)
if gs_store is not None:
gs_layer = cat.get_layer(name)
if gs_layer is not None:
gs_resource = gs_layer.resource
else:
gs_layer = None
gs_resource = None
def get_default_user():
"""Create a default user
"""
superusers = get_user_model().objects.filter(
is_superuser=True).order_by('id')
if superusers.count() > 0:
# Return the first created superuser
return superusers[0]
else:
raise GeoNodeException('You must have an admin account configured '
'before importing data. '
def get_or_create_datastore(cat, workspace=None, charset="UTF-8"):
"""
Get a PostGIS database store or create it in GeoServer if does not exist.
"""
# TODO refactor this and geoserver.helpers._create_db_featurestore
# dsname = ogc_server_settings.DATASTORE
dsname = ogc_server_settings.datastore_db['NAME']
if not ogc_server_settings.DATASTORE:
msg = ("To use the createlayer application you must set ogc_server_settings.datastore_db['ENGINE']"
" to 'django.contrib.gis.db.backends.postgis")
logger.error(msg)
raise GeoNodeException(msg)
try:
ds = cat.get_store(dsname, workspace=workspace)
except FailedRequestError:
ds = cat.create_datastore(dsname, workspace=workspace)
db = ogc_server_settings.datastore_db
ds.connection_parameters.update(
{'validate connections': 'true',
'max connections': '10',
'min connections': '1',
'fetch size': '1000',
'host': db['HOST'],
'port': db['PORT'] if isinstance(
db['PORT'], string_types) else str(db['PORT']) or '5432',
'database': db['NAME'],
def get_valid_layer_name(layer, overwrite):
"""Checks if the layer is a string and fetches it from the database.
"""
# The first thing we do is get the layer name string
if isinstance(layer, Layer):
layer_name = layer.name
elif isinstance(layer, basestring):
layer_name = str(layer)
else:
msg = ('You must pass either a filename or a GeoNode layer object')
raise GeoNodeException(msg)
if overwrite:
return layer_name
else:
return get_valid_name(layer_name)