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)
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)
def editor(): export_form = ExportVectorForm() export_form.srs.choices = list( current_app.config.geobox_state.config.get('web', 'available_srs')) user = current_app.config.geobox_state.user target_box_name = 'file_box' if user.is_consultant else 'upload_box' target_box_label = _('filebox') if user.is_consultant else _('upload_box') target_box = current_app.config.geobox_state.config.get( 'couchdb', target_box_name) export_form.destination.choices = [('file', _('Filesystem')), (target_box, target_box_label)] # load preview layer preview_features = False preview_layername = False box_name = request.args.get('box_name', False) filename = request.args.get('filename', False) if box_name and filename: couchbox = get_couch_box_db(box_name) couch_src = CouchFileBox( 'http://%s:%s' % ('127.0.0.1', current_app.config.geobox_state.config.get('couchdb', 'port')), couchbox) preview_features = couch_src.get_attachment(filename) preview_layername = "%s (%s)" % (filename, _('Temporary')) base_layers = g.db.query(ExternalWMTSSource).filter_by( background_layer=True).all() for base_layer in base_layers: base_layer.bbox = base_layer.bbox_from_view_coverage() wfs_search_sources = g.db.query(ExternalWFSSource).filter_by( active=True).all() if not wfs_search_sources: wfs_search_sources = False parcel_search_sources = g.db.query(ParcelSearchSource).filter_by( active=True).all() if not parcel_search_sources: parcel_search_sources = False server_search_form = ServerSearchForm() return render_template( 'editor.html', base_layers=base_layers, export_form=export_form, preview_layername=preview_layername, preview_features=preview_features, parcel_service=True, wfs_search_sources=wfs_search_sources, parcel_search_sources=parcel_search_sources, server_search_form=server_search_form, with_server=True, wms_search_url=current_app.config.geobox_state.config.get( 'web', 'wms_search_url'), is_local=request_is_local())
def check_file_exists(box_name): couch_box = get_couch_box_db(box_name) couch = CouchFileBox('http://127.0.0.1:%s' % current_app.config.geobox_state.config.get('couchdb', 'port'), couch_box) existing_doc = couch.get(request.form['filename']) if existing_doc: return jsonify(existing=True) return jsonify(existing=False)
def editor(): export_form = ExportVectorForm() export_form.srs.choices = list(current_app.config.geobox_state.config.get('web', 'available_srs')) user = current_app.config.geobox_state.user target_box_name = 'file_box' if user.is_consultant else 'upload_box' target_box_label = _('filebox') if user.is_consultant else _('upload_box') target_box = current_app.config.geobox_state.config.get('couchdb', target_box_name) export_form.destination.choices = [('file', _('Filesystem')), (target_box, target_box_label)] # load preview layer preview_features = False preview_layername = False box_name = request.args.get('box_name', False) filename = request.args.get('filename', False) if box_name and filename: couchbox = get_couch_box_db(box_name) couch_src = CouchFileBox('http://%s:%s' % ('127.0.0.1', current_app.config.geobox_state.config.get('couchdb', 'port')), couchbox) preview_features = couch_src.get_attachment(filename) preview_layername = "%s (%s)" % (filename, _('Temporary')) base_layers = g.db.query(ExternalWMTSSource).filter_by(background_layer=True).all() for base_layer in base_layers: base_layer.bbox = base_layer.bbox_from_view_coverage() wfs_search_sources = g.db.query(ExternalWFSSource).filter_by(active=True).all() if not wfs_search_sources: wfs_search_sources = False parcel_search_sources = g.db.query(ParcelSearchSource).filter_by(active=True).all() if not parcel_search_sources: parcel_search_sources = False server_search_form = ServerSearchForm() return render_template('editor.html', base_layers=base_layers, export_form=export_form, preview_layername=preview_layername, preview_features=preview_features, parcel_service=True, wfs_search_sources=wfs_search_sources, parcel_search_sources=parcel_search_sources, server_search_form=server_search_form, with_server=True, wms_search_url=current_app.config.geobox_state.config.get('web', 'wms_search_url'), is_local=request_is_local() )
def export_selected_geometries(): user = User(current_app.config.geobox_state.config.get('user', 'type')) target_box_name = 'file_box' if user.is_consultant else 'upload_box' target_box = current_app.config.geobox_state.config.get('couchdb', target_box_name) filename = request.form.get('filename', False) data = request.form.get('geojson', False) file_obj = {'content-type': 'application/json' , 'file': data, 'filename': filename + '.json' } couch = CouchFileBox('http://%s:%s' % ('127.0.0.1', current_app.config.geobox_state.config.get('couchdb', 'port')), target_box) try: couch.store_file(file_obj, overwrite=True) except Exception, e: log.exception(e) abort(500)
def files(box_name, user_id=None): is_consultant = current_app.config.geobox_state.user.is_consultant if (box_name == 'file' and not is_consultant): raise NotFound() if ((box_name == 'download' or box_name == 'upload') and is_consultant): raise NotFound() form = UploadForm() import_form = ImportGeoJSONEdit() import_form = prepare_geojson_form(import_form) couch_box = get_couch_box_db(box_name) host = request.headers.get('Host').split(':')[0] couch = CouchFileBox( 'http://%s:%s' % ( host, current_app.config.geobox_state.config.get('couchdb', 'port') ), couch_box) if form.validate_on_submit(): file = request.files['file'] overwrite = True if request.form.get('overwrite') == 'true' else False if file: data = get_file_information(file) if data: couch.store_file(data, overwrite=overwrite) flash(_('upload success'), 'success') else: flash(_('file type not allowed'), 'error') files = couch.all_files() for f in files: f['download_link'] = couchid_to_link(f['id'], couch_url=couch.couch_url) return render_template("boxes/%s.html" % box_name, form=form, files=files, box_name=box_name, import_form=import_form)
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)
def delete_file(box_name, id, rev): couch_box = get_couch_box_db(box_name) couch = CouchFileBox('http://127.0.0.1:%s' % current_app.config.geobox_state.config.get('couchdb', 'port'), couch_box) couch.delete(id, rev) flash(_("file deleted"), 'success') return redirect(url_for("boxes.files", box_name=box_name))
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)