def download_file(export_dir, filename): export_path = current_app.config.geobox_state.user_data_path('export') download_path = join(export_path, export_dir) +"/"+filename if '.shp' in filename: download_path = join(export_path, export_dir) +"/"+filename zip_file = zip_shapefiles(download_path) download_file = zip_file.getvalue() filename = filename.replace('.shp', '.zip') else: download_file = open(download_path, 'rb') return Response(download_file, direct_passthrough=True, headers={'Content-disposition': 'attachment; filename=%s' % (filename)})
def download_file(export_dir, filename): export_path = current_app.config.geobox_state.user_data_path('export') download_path = join(export_path, export_dir) +"/"+filename if '.shp' in filename: download_path = join(export_path, export_dir) +"/"+filename zip_file = zip_shapefiles(download_path) download_file = zip_file.getvalue() filename = filename.replace('.shp', '.zip') else: download_file = open(download_path, 'rb') return Response(download_file, direct_passthrough=True, headers={ 'Content-disposition': 'attachment; filename=%s' % (filename), 'Content-type': 'application/octet-stream', })
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)