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 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)