Example #1
0
 def process(self):
     log.debug('Start vector import process. Task %d' % self.task_id)
     try:
         with self.task() as task:
             mapping = Mapping(None, None, '*', other_srs=task.srs)
             couch = VectorCouchDB(
                 'http://%s:%s' %
                 ('127.0.0.1', self.app_state.config.get(
                     'couchdb', 'port')), task.db_name, task.title)
             # import from file
             if task.source == 'file':
                 input_file = self.app_state.user_data_path(
                     'import', task.file_name)
                 if task.type_ == 'geojson':
                     records = json.loads(open(input_file).read())
                     couch.store_records(records['features'])
                 elif task.type_ == 'gml':
                     couch.store_records(
                         load_json_from_gml(input_file, mapping))
                 elif task.type_ == 'shp':
                     couch.store_records(
                         load_json_from_shape(input_file, mapping))
             # import from couch db - source name is couchdb name
             else:
                 couch_src = CouchFileBox(
                     'http://%s:%s' %
                     ('127.0.0.1',
                      self.app_state.config.get('couchdb', 'port')),
                     task.source)
                 records = couch_src.get_attachment(task.file_name)
                 couch.store_records(records['features'])
         self.task_done()
     except ConvertError, e:
         self.task_failed(e)
Example #2
0
def proxy_request(url):
    app_state = current_app.config.geobox_state

    response = proxy_couchdb_request(request, url)
    if '201' in response.status:

        if (
            app_state.config.get('web', 'authorization_layer_name') in url
            and 'metadata' not in url
        ):
            couch_url = 'http://%s:%s' % (
                '127.0.0.1',
                app_state.config.get('couchdb', 'port')
            )
            db_name = '%s_%s' % (
                app_state.home_server.prefix,
                app_state.config.get('web', 'authorization_layer_name')
            )
            couch = VectorCouchDB(couch_url, db_name)
            download_coverage = couch.coverage()

            db_session = app_state.user_db_session()
            sources = db_session.query(ExternalWMTSSource).filter_by(
                is_public=False).all()
            for source in sources:
                source.download_coverage = json.dumps(download_coverage)
            db_session.commit()

            if app_state.home_server is not None:
                requests.get(app_state.home_server.update_coverage_url)

    return response
Example #3
0
def proxy_request(url):
    app_state = current_app.config.geobox_state

    response = proxy_couchdb_request(request, url)
    if '201' in response.status:

        if (app_state.config.get('web', 'authorization_layer_name') in url
                and 'metadata' not in url):
            couch_url = 'http://%s:%s' % (
                '127.0.0.1', app_state.config.get('couchdb', 'port'))
            db_name = '%s_%s' % (app_state.home_server.prefix,
                                 app_state.config.get(
                                     'web', 'authorization_layer_name'))
            couch = VectorCouchDB(couch_url, db_name)
            download_coverage = couch.coverage()

            db_session = app_state.user_db_session()
            sources = db_session.query(ExternalWMTSSource).filter_by(
                is_public=False).all()
            for source in sources:
                source.download_coverage = json.dumps(download_coverage)
            db_session.commit()

            if app_state.home_server is not None:
                requests.get(app_state.home_server.update_coverage_url)

    return response
Example #4
0
 def process(self):
     log.debug('Start vector import process. Task %d' % self.task_id)
     try:
         with self.task() as task:
             mapping = Mapping(None, None, '*', other_srs=task.srs)
             couch = VectorCouchDB('http://%s:%s' % ('127.0.0.1', self.app_state.config.get('couchdb', 'port')), task.db_name, task.title)
             # import from file
             if task.source == 'file':
                 input_file = self.app_state.user_data_path('import', task.file_name)
                 if task.type_ == 'geojson':
                     records = json.loads(open(input_file).read())
                     couch.store_records(
                         records['features']
                     )
                 elif task.type_ == 'gml':
                     couch.store_records(
                         load_json_from_gml(input_file, mapping)
                     )
                 elif task.type_ == 'shp':
                     couch.store_records(
                         load_json_from_shape(input_file, mapping)
                     )
             # import from couch db - source name is couchdb name
             else:
                 couch_src = CouchFileBox('http://%s:%s' % ('127.0.0.1', self.app_state.config.get('couchdb', 'port')), task.source)
                 records = couch_src.get_attachment(task.file_name)
                 couch.store_records(
                     records['features']
                 )
         self.task_done()
     except ConvertError, e:
         self.task_failed(e)
Example #5
0
def local_raster_remove(id):
    raster_source = g.db.query(LocalWMTSSource).with_polymorphic('*').filter_by(id=id).first()

    if not raster_source:
        abort(404)

    couch_url = 'http://%s:%s' % ('127.0.0.1', current_app.config.geobox_state.config.get('couchdb', 'port'))
    couch = VectorCouchDB(couch_url, raster_source.name, raster_source.name)
    couch.delete_db()

    g.db.delete(raster_source)
    g.db.commit()
    flash( _('delete local source successful'), 'success')
    return redirect(url_for('.raster_list'))
Example #6
0
    def _check_metadata_doc(self, layer, source):
        couchdb_layer = '%s%s' % (self.prefix, layer)

        source_md = source.load_record('schema_' + layer)
        title = source_md.get('title', layer) if source_md else layer

        couch = VectorCouchDB(self.url, couchdb_layer, title)

        md_doc = {
            'title': title,
            'name': couchdb_layer,
            'layer': source_md.get('layer', layer) if source_md else layer,
            'type': 'GeoJSON',
        }
        couch.update_or_create_doc('metadata', md_doc)
Example #7
0
    def _check_metadata_doc(self, layer, source):
        couchdb_layer = '%s%s' % (self.prefix, layer)

        source_md = source.load_record('schema_' + layer)
        title = source_md.get('title', layer) if source_md else layer

        couch = VectorCouchDB(self.url, couchdb_layer, title)

        md_doc = {
            'title': title,
            'name': couchdb_layer,
            'layer': source_md.get('layer', layer) if source_md else layer,
            'type': 'GeoJSON',
        }
        couch.update_or_create_doc('metadata', md_doc)
Example #8
0
def local_raster_remove(id):
    raster_source = g.db.query(LocalWMTSSource).with_polymorphic(
        '*').filter_by(id=id).first()

    if not raster_source:
        abort(404)

    couch_url = 'http://%s:%s' % ('127.0.0.1',
                                  current_app.config.geobox_state.config.get(
                                      'couchdb', 'port'))
    couch = VectorCouchDB(couch_url, raster_source.name, raster_source.name)
    couch.delete_db()

    g.db.delete(raster_source)
    g.db.commit()
    flash(_('delete local source successful'), 'success')
    return redirect(url_for('.raster_list'))
Example #9
0
    def insert(self, feature):
        couchdb_layer = '%s%s' % (self.prefix, feature.layer)
        couch = self._dbs.get(couchdb_layer, None)
        if not couch:
            couch = VectorCouchDB(self.url, couchdb_layer, couchdb_layer)
            self._dbs[couchdb_layer] = couch
        feature_dict = {
            'geometry': feature.geometry,
            'properties': feature.properties,
            'type': 'Feature',
        }
        existing_feature = couch.get(feature.id)
        if existing_feature:
            feature_dict['_rev'] = existing_feature['_rev']

        couch.put(feature.id, feature_dict)

        self.inserted_layers.add(feature.layer)
Example #10
0
    def insert(self, feature):
        couchdb_layer = '%s%s' % (self.prefix, feature.layer)
        couch = self._dbs.get(couchdb_layer, None)
        if not couch:
            couch = VectorCouchDB(self.url, couchdb_layer, couchdb_layer)
            self._dbs[couchdb_layer] = couch
        feature_dict = {
            'geometry': feature.geometry,
            'properties': feature.properties,
            'type': 'Feature',
        }
        existing_feature = couch.get(feature.id)
        if existing_feature:
            feature_dict['_rev'] = existing_feature['_rev']

        couch.put(feature.id, feature_dict)

        self.inserted_layers.add(feature.layer)
Example #11
0
def upload_gml():
    app_state = current_app.config.geobox_state

    form = forms.GMLUploadForm()
    form.srs.choices = list(app_state.config.get('web', 'available_srs'))
    form.srs.choices.insert(0, ('', _('-- select srs --'), ''))

    if form.validate_on_submit():
        upload_file = request.files['upload_file']
        if upload_file:
            mapping = Mapping(None, None, '*', other_srs=form.srs.data)
            couch_url = 'http://%s:%s' % (
                '127.0.0.1', app_state.config.get('couchdb', 'port'))
            db_name = '%s_%s' % (app_state.home_server.prefix,
                                 app_state.config.get(
                                     'web', 'authorization_layer_name'))
            couch = VectorCouchDB(
                couch_url, db_name,
                app_state.config.get('web', 'authorization_layer_title'))
            couch.remove_all_features()
            couch.store_records(load_json_from_gml(upload_file, mapping))

            download_coverage = couch.coverage()

            db_session = app_state.user_db_session()
            sources = db_session.query(ExternalWMTSSource).filter_by(
                is_public=False).all()
            for source in sources:
                source.download_coverage = json.dumps(download_coverage)
            db_session.commit()

            flash(
                _('gml file %(name)s successfully uploaded and geometries added to %(auth_layer_title)s',
                  name=upload_file.filename,
                  auth_layer_title=app_state.config.get(
                      'web', 'authorization_layer_title')), 'info')
            return redirect(url_for('main.index'))

    return render_template('admin/upload_gml.html', form=form)
Example #12
0
    def process(self):
        log.debug('Start vector export process. Task %d' % self.task_id)
        try:
            with self.task() as task:
                couch = VectorCouchDB('http://%s:%s' % ('127.0.0.1', self.app_state.config.get('couchdb', 'port')), task.db_name, task.title)

                if task.type_ == 'geojson':
                    # use geojson if is in task - otherwise load from database
                    if not task.geojson:
                        data = json.dumps(create_feature_collection(couch.load_features()))
                    else:
                        data = task.geojson

                    # check metadata for jsonSchema and restrict feature properties to
                    # schema properties of schema don't allow additional properties
                    metadata = couch.metadata()
                    if metadata and 'appOptions' in metadata and 'jsonSchema' in metadata['appOptions'] and 'additionalProperties' in metadata['appOptions']['jsonSchema']['schema'] and metadata['appOptions']['jsonSchema']['schema']['additionalProperties'] == False:
                        schema_properties = metadata['appOptions']['jsonSchema']['schema']['properties'].keys()
                        data = json.loads(data)
                        for feature in data['features']:
                            export_properties = {}
                            for key in feature['properties'].keys():
                                if key in schema_properties:
                                    export_properties[key] = feature['properties'][key]
                            feature['properties'] = export_properties
                        data = json.dumps(data)

                    if task.destination != 'file':
                        dest_couch = CouchFileBox('http://%s:%s' % ('127.0.0.1', self.app_state.config.get('couchdb', 'port')), task.destination)

                        file_obj = {'content-type': 'application/json' , 'file': data, 'filename': task.file_name + '.json' }
                        dest_couch.store_file(file_obj, overwrite=True)
                    else:
                        output_file = self.app_state.user_data_path('export', 'vector', task.file_name + '.json', make_dirs=True)
                        write_json_to_file(json.loads(data), output_file)

                elif task.type_ == 'shp':
                    if task.destination != 'file':
                        dest_couch = CouchFileBox('http://%s:%s' % ('127.0.0.1', self.app_state.config.get('couchdb', 'port')), task.destination)
                        temp_dir = mkdtemp()

                        output_file = path.join(temp_dir, task.file_name + '.shp')
                        fields = fields_from_properties(couch.load_features())
                        mapping = Mapping(None, None, 'Polygon', other_srs=task.srs, fields=fields)

                        # create shape
                        write_json_to_shape(couch.load_features(), mapping, output_file)
                        zip_file = zip_shapefiles(output_file)
                        dest_couch.store_file({'content-type': 'application/zip', 'file': zip_file.getvalue(), 'filename': task.file_name + '.zip' }, overwrite=True)

                        rmtree(temp_dir)
                    else:
                        output_file = self.app_state.user_data_path('export',  'vector', task.file_name+ '.shp', make_dirs=True)
                        # create fields for shp - use for mapping
                        fields = fields_from_properties(couch.load_features())
                        mapping = Mapping(None, None, 'Polygon', other_srs=task.srs, fields=fields)
                        # create shape
                        write_json_to_shape(couch.load_features(), mapping, output_file)
            self.task_done()
        except ConvertError, e:
            self.task_failed(e)
Example #13
0
    def process(self):
        log.debug('Start vector export process. Task %d' % self.task_id)
        try:
            with self.task() as task:
                couch = VectorCouchDB(
                    'http://%s:%s' %
                    ('127.0.0.1', self.app_state.config.get(
                        'couchdb', 'port')), task.db_name, task.title)

                if task.type_ == 'geojson':
                    # use geojson if is in task - otherwise load from database
                    if not task.geojson:
                        data = json.dumps(
                            create_feature_collection(couch.load_features()))
                    else:
                        data = task.geojson

                    # check metadata for jsonSchema and restrict feature properties to
                    # schema properties of schema don't allow additional properties
                    metadata = couch.metadata()
                    if metadata and 'appOptions' in metadata and 'jsonSchema' in metadata[
                            'appOptions'] and 'additionalProperties' in metadata[
                                'appOptions']['jsonSchema'][
                                    'schema'] and metadata['appOptions'][
                                        'jsonSchema']['schema'][
                                            'additionalProperties'] == False:
                        schema_properties = metadata['appOptions'][
                            'jsonSchema']['schema']['properties'].keys()
                        data = json.loads(data)
                        for feature in data['features']:
                            export_properties = {}
                            for key in feature['properties'].keys():
                                if key in schema_properties:
                                    export_properties[key] = feature[
                                        'properties'][key]
                            feature['properties'] = export_properties
                        data = json.dumps(data)

                    if task.destination != 'file':
                        dest_couch = CouchFileBox(
                            'http://%s:%s' %
                            ('127.0.0.1',
                             self.app_state.config.get('couchdb', 'port')),
                            task.destination)

                        file_obj = {
                            'content-type': 'application/json',
                            'file': data,
                            'filename': task.file_name + '.json'
                        }
                        dest_couch.store_file(file_obj, overwrite=True)
                    else:
                        output_file = self.app_state.user_data_path(
                            'export',
                            'vector',
                            task.file_name + '.json',
                            make_dirs=True)
                        write_json_to_file(json.loads(data), output_file)

                elif task.type_ == 'shp':
                    if task.destination != 'file':
                        dest_couch = CouchFileBox(
                            'http://%s:%s' %
                            ('127.0.0.1',
                             self.app_state.config.get('couchdb', 'port')),
                            task.destination)
                        temp_dir = mkdtemp()

                        output_file = path.join(temp_dir,
                                                task.file_name + '.shp')
                        fields = fields_from_properties(couch.load_features())
                        mapping = Mapping(None,
                                          None,
                                          'Polygon',
                                          other_srs=task.srs,
                                          fields=fields)

                        # create shape
                        write_json_to_shape(couch.load_features(), mapping,
                                            output_file)
                        zip_file = zip_shapefiles(output_file)
                        dest_couch.store_file(
                            {
                                'content-type': 'application/zip',
                                'file': zip_file.getvalue(),
                                'filename': task.file_name + '.zip'
                            },
                            overwrite=True)

                        rmtree(temp_dir)
                    else:
                        output_file = self.app_state.user_data_path(
                            'export',
                            'vector',
                            task.file_name + '.shp',
                            make_dirs=True)
                        # create fields for shp - use for mapping
                        fields = fields_from_properties(couch.load_features())
                        mapping = Mapping(None,
                                          None,
                                          'Polygon',
                                          other_srs=task.srs,
                                          fields=fields)
                        # create shape
                        write_json_to_shape(couch.load_features(), mapping,
                                            output_file)
            self.task_done()
        except ConvertError, e:
            self.task_failed(e)