Esempio n. 1
0
    def testDimensionInfo(self):
        inf = DimensionInfo( * (None,) * 6 )
        # make sure these work with no resolution set
        self.assertTrue(inf.resolution_millis() is None)
        self.assertTrue(inf.resolution_str() is None)

        inf = lambda r: DimensionInfo(None, None, None, r, None, None)
        def assertEqualResolution(spec, millis):
            self.assertEqual(millis, inf(spec).resolution_millis())
            self.assertEqual(spec, inf(millis).resolution_str())

        assertEqualResolution('0.5 seconds', 500)
        assertEqualResolution('7 days', 604800000)
        assertEqualResolution('10 years', 315360000000000)
Esempio n. 2
0
    def testDimensionInfo(self):
        inf = DimensionInfo( * (None,) * 6 )
        # make sure these work with no resolution set
        self.assertTrue(inf.resolution_millis() is None)
        self.assertTrue(inf.resolution_str() is None)

        inf = lambda r: DimensionInfo(None, None, None, r, None, None)
        def assertEqualResolution(spec, millis):
            self.assertEqual(millis, inf(spec).resolution_millis())
            self.assertEqual(spec, inf(millis).resolution_str())

        assertEqualResolution('0.5 seconds', 500)
        assertEqualResolution('7 days', 604800000)
        assertEqualResolution('10 years', 315360000000000)
Esempio n. 3
0
def configure_time(resource, name='time', enabled=True, presentation='LIST', resolution=None, units=None,
                   unitSymbol=None, **kwargs):
    """
    Configures time on a geoserver resource.
    """
    time_info = DimensionInfo(name, enabled, presentation, resolution, units, unitSymbol, **kwargs)
    resource.metadata = {'time': time_info}
    return resource.catalog.save(resource)
Esempio n. 4
0
    def testTimeDimension(self):
        sf = self.cat.get_workspace("sf")
        files = shapefile_and_friends(
            os.path.join(gisdata.GOOD_DATA, "time", "boxes_with_end_date"))
        self.cat.create_featurestore("boxes_with_end_date", files, sf)

        get_resource = lambda: self.cat._cache.clear() or self.cat.get_layer(
            'boxes_with_end_date').resource

        # configure time as LIST
        resource = get_resource()
        timeInfo = DimensionInfo("time",
                                 "true",
                                 "LIST",
                                 None,
                                 "ISO8601",
                                 None,
                                 attribute="date")
        resource.metadata = {'time': timeInfo}
        self.cat.save(resource)
        # and verify
        resource = get_resource()
        timeInfo = resource.metadata['time']
        self.assertEqual("LIST", timeInfo.presentation)
        self.assertEqual(True, timeInfo.enabled)
        self.assertEqual("date", timeInfo.attribute)
        self.assertEqual("ISO8601", timeInfo.units)

        # disable time dimension
        timeInfo = resource.metadata['time']
        timeInfo.enabled = False
        # since this is an xml property, it won't get written unless we modify it
        resource.metadata = {'time': timeInfo}
        self.cat.save(resource)
        # and verify
        resource = get_resource()
        timeInfo = resource.metadata['time']
        self.assertEqual(False, timeInfo.enabled)

        # configure with interval, end_attribute and enable again
        timeInfo.enabled = True
        timeInfo.presentation = 'DISCRETE_INTERVAL'
        timeInfo.resolution = '3 days'
        timeInfo.end_attribute = 'enddate'
        resource.metadata = {'time': timeInfo}
        self.cat.save(resource)
        # and verify
        resource = get_resource()
        timeInfo = resource.metadata['time']
        self.assertEqual(True, timeInfo.enabled)
        self.assertEqual('DISCRETE_INTERVAL', timeInfo.presentation)
        self.assertEqual('3 days', timeInfo.resolution_str())
        self.assertEqual('enddate', timeInfo.end_attribute)
Esempio n. 5
0
def _add_time_dimension(coverage, time_dim):
    """Add time dimension for the layer."""
    metadata = coverage.metadata.copy()
    time_info = DimensionInfo(
        time_dim["name"],
        time_dim["enabled"],
        time_dim["presentation"],
        time_dim["resolution"],
        time_dim["units"],
        None,
        nearestMatchEnabled=time_dim["nearestMatchEnabled"])
    metadata['time'] = time_info
    coverage.metadata = metadata

    return coverage
Esempio n. 6
0
    def testTimeDimension(self):
        sf = self.cat.get_workspace("sf")
        files = shapefile_and_friends(os.path.join(gisdata.GOOD_DATA, "time", "boxes_with_end_date"))
        self.cat.create_featurestore("boxes_with_end_date", files, sf)

        get_resource = lambda: self.cat._cache.clear() or self.cat.get_layer('boxes_with_end_date').resource

        # configure time as LIST
        resource = get_resource()
        timeInfo = DimensionInfo("time", "true", "LIST", None, "ISO8601", None, attribute="date")
        resource.metadata = {'time':timeInfo}
        self.cat.save(resource)
        # and verify
        resource = get_resource()
        timeInfo = resource.metadata['time']
        self.assertEqual("LIST", timeInfo.presentation)
        self.assertEqual(True, timeInfo.enabled)
        self.assertEqual("date", timeInfo.attribute)
        self.assertEqual("ISO8601", timeInfo.units)

        # disable time dimension
        timeInfo = resource.metadata['time']
        timeInfo.enabled = False
        # since this is an xml property, it won't get written unless we modify it
        resource.metadata = {'time' : timeInfo}
        self.cat.save(resource)
        # and verify
        resource = get_resource()
        timeInfo = resource.metadata['time']
        self.assertEqual(False, timeInfo.enabled)

        # configure with interval, end_attribute and enable again
        timeInfo.enabled = True
        timeInfo.presentation = 'DISCRETE_INTERVAL'
        timeInfo.resolution = '3 days'
        timeInfo.end_attribute = 'enddate'
        resource.metadata = {'time' : timeInfo}
        self.cat.save(resource)
        # and verify
        resource = get_resource()
        timeInfo = resource.metadata['time']
        self.assertEqual(True, timeInfo.enabled)
        self.assertEqual('DISCRETE_INTERVAL', timeInfo.presentation)
        self.assertEqual('3 days', timeInfo.resolution_str())
        self.assertEqual('enddate', timeInfo.end_attribute)
Esempio n. 7
0
    logging.info("Uploading " + str(len(files)) + " styles")
    for f in files:
        with open(styleDir + f, 'r') as style:
            styleName = f.rstrip('.xml')
            cat.create_style(styleName, style.read(), workspace=workspace, overwrite=True)
            styles.append(workspace + ':' + styleName)
    break

# assign styles to created layers and enable timeDimension
layers = cat.get_layers()
for layer in layers:
    # if layer.dom is not None:
    if layer.name.startswith(workspace):
        # GetStyleName
        layerName = layer.resource.name
        # Set Default Style (timeIndependend)
        layer.default_style = workspace + ":" + layerName
        layer.styles = [s for s in styles if s.startswith(workspace + ":" + layerName)]
        cat.save(layer)
        # get coverage to activate time Dimension
        from geoserver.support import DimensionInfo
        coverage = cat.get_resource(layerName, projectName, workspace=workspace)
        timeInfo = DimensionInfo("time", "true", "LIST", None, "ISO8601", None)
        coverage.metadata = ({'time': timeInfo})
        coverage.CheckAuxiliaryMetadata=True
        cat.save(coverage)
if 'removeOutputFiles' in cfg['general'] and cfg['general']['removeOutputFiles'] is not False:
    shutil.rmtree(cfg['general']['workdir'] + '/outputFiles/' + projectName + '/')
    logging.info("Deleted outputFiles")
logging.info('App can be started at: ' + cfg['frontend']['path'] + '/projects/' + projectName + '/index.html')
Esempio n. 8
0
def layer_create(request, data=None, template='upload/layer_create.html'):
    print 'layer create'
    if request.method == 'POST':
        feature_type = json.loads(request.POST.get(u'featureType', None))
        datastore = feature_type['store']['name']
        store_create_geogig = json.loads(
            request.POST.get(u'storeCreateGeogig', 'false'))

        if store_create_geogig:
            if not feature_type['store']['name']:
                store_proposed = slugify(' '.join(
                    [request.user.username, feature_type['name']]))
            store_created = create_geoserver_db_featurestore(
                store_type='geogig', store_name=store_proposed)
            feature_type['store']['name'] = store_created.name

        # -- only allow creation of layers in the whitelisted datastores
        try:
            settings.ALLOWED_DATASTORE_LAYER_CREATE
        except AttributeError:
            return HttpResponse(
                status=FORBIDDEN,
                content=
                'ALLOWED_DATASTORE_LAYER_CREATE whitelist is not defined.')

        if datastore not in settings.ALLOWED_DATASTORE_LAYER_CREATE:
            return HttpResponse(
                status=FORBIDDEN,
                content=
                'datastore specified in featureType is not in the ALLOWED_DATASTORE_LAYER_CREATE whitelist.'
            )

        post_request = requests.post(
            '{}/workspaces/{}/datastores/{}/featuretypes.json'.format(
                ogc_server_settings.rest, feature_type['namespace']['name'],
                datastore),
            data='{{"featureType":{}}}'.format(json.dumps(feature_type)),
            auth=ogc_server_settings.credentials,
            headers={'content-type': 'application/json'})

        if post_request.status_code == 200 or post_request.status_code == 201:
            # import the layer from geoserver to geonode
            response = gs_slurp(filter=feature_type['name'],
                                workspace=feature_type['namespace']['name'],
                                store=feature_type['store']['name'])
            if 'layers' in response and len(
                    response['layers']
            ) == 1 and 'name' in response['layers'][0] and response['layers'][
                    0]['name'] == feature_type['name']:
                # configruer layer with time dimension as a list for now. Since importer is being refactored,
                # we'll change how it is done.
                layer = gs_catalog.get_layer(feature_type['name'])
                resource = layer.resource
                if layer:
                    time_info = DimensionInfo(name='time',
                                              enabled=True,
                                              presentation='LIST',
                                              resolution=None,
                                              units=None,
                                              unitSymbol=None,
                                              attribute='time',
                                              end_attribute=None,
                                              strategy=None)
                    resource.metadata = {'time': time_info}
                    resource.catalog.save(resource)
                    return HttpResponse(status=post_request.status_code,
                                        content=post_request.text)
                return HttpResponse(status=500,
                                    content='failed to configure layer')
            else:
                return HttpResponse(
                    status=500,
                    content=
                    'failed to add created layer from geoserver to geonode')
        else:
            return HttpResponse(status=post_request.status_code,
                                content=post_request.text)
        print '---- create layer response: ', post_request.text
    return render_to_response(template, RequestContext(request, {}))
Esempio n. 9
0
    def create_layer(self):
        """
        Constructs the layer details and creates it in the geoserver.
        If the layer already exists then return the pre-existing layer.
        Layer "existence" is based entirely on the layer's name -- it must be unique

        @returns geoserver layer
        """

        # If the layer already exists in Geoserver then return it
        if self.resource_id.endswith("_multi"):
            layer = self.geoserver.get_layer(self.getName())
        else:
            layer = self.geoserver.get_layer(self.name)
        layer_workspace_name = None
        if layer:
            layer_workspace_name = str(layer.resource._workspace).replace(' ', '').split('@')[0]

        if not layer or (layer_workspace_name and layer_workspace_name != self.workspace_name):

            if isinstance(self.data, RasterFile):
                ws = self.geoserver.default_workspace()
                resource = toolkit.get_action("resource_show")(None, {
                    "id": self.resource_id
                    })
                if resource.get("format", {}) == "geotiff":
                    url_ = resource.get("url", {})
                    label = url_.rsplit('/', 1)[-1]
                    self.geoserver.create_coveragestore_external_geotiff(self.getName(), "file:///var/tmp/GeoserverUpload/" + self.package_id + "/" + label, ws, overwrite=True)
                    layer = self.geoserver.get_layer(self.name)

            elif isinstance(self.data, MultiRasterFile):
                ws = self.geoserver.default_workspace()
                label = toolkit.get_action("package_show")(None, {
                    "id": self.package_id
                    }).get("name", self.package_id)

                self.geoserver.create_imagemosaic(self.getName(), self.data.zipFileLocation, workspace=ws, overwrite=True)

                coverage = self.geoserver.get_resource_by_url(ws.coveragestore_url.replace(".xml", "/" + self.getName() + "/coverages/" + self.getName() + ".xml"))

                coverage.supported_formats = ['GEOTIFF']
                coverage.title = label
                timeInfo = DimensionInfo("time", "true", "LIST", None, "ISO8601", None)
                coverage.metadata = ({
                    'dirName': self.getName() + "_" + self.getName(),
                    'time': timeInfo
                    })
                self.geoserver.save(coverage)
                layer = self.geoserver.get_layer(self.getName())
            else:
                # Construct layer creation request.
                feature_type_url = url(self.geoserver.service_url, ["workspaces", self.store.workspace.name, "datastores", self.store.name, "featuretypes"])

                if self.resource_id.endswith("_multi"):
                    description = self.file_resource["notes"]
                else:
                    description = self.file_resource["description"]

                data = {
                    "featureType": {
                        "name": self.getName(),
                        "nativeName": self.getName(),
                        "title": self.file_resource["name"],
                        "abstract": description
                        }
                    }

                request_headers = {
                    "Content-type": "application/json"
                    }

                response_headers, response = self.geoserver.http.request(feature_type_url, "POST", json.dumps(data), request_headers)
                if not "already exists in store" in response:
                    if not 200 <= response_headers.status < 300:
                        raise Exception(toolkit._("Geoserver layer creation failed: %i -- %s") % (response_headers.status, response))

                layer = self.geoserver.get_layer(self.name)
            return layer

        # Add the layer's name to the file resource
        self.file_resource.update({
            "layer_name": self.name
            })
        self.file_resource = toolkit.get_action("resource_patch")({
            "user": self.username
            }, self.file_resource)
        # Return the layer
        return layer