def prepare_layer_list(form): couch_url = 'http://%s:%s' % ( '127.0.0.1', current_app.config.geobox_state.config.get('couchdb', 'port')) form.layers.choices = [ (item['dbname'], item['title']) for item in vector_layers_metadata(couch_url) ] form.layers.choices.insert(0, ('', _('-- select layer or add new --'))) return form
def import_edit(id=None): sources = g.db.query(model.ExternalWMTSSource).all() if id is None: proj = model.ImportProject() else: proj = g.db.query(model.ImportProject).get(id) if not proj: abort(404) coverage_form = forms.SelectCoverage() form = forms.ImportProjectEdit(request.form) form.start_level.choices = get_levels(model.ExternalWMTSSource) form.end_level.choices = get_levels(model.ExternalWMTSSource) form.update_tiles.checked = proj.update_tiles if form.validate_on_submit() and validate_max_tiles(form): redirect_url = url_for('.import_list', id=id) proj.title = form.data['title'] proj.import_raster_layers = [] if form.coverage.data != 'false': proj.coverage = prepare_feature_collection_coverage( form.coverage.data) else: proj.coverage = '' coverage = None proj.download_size = float(form.data.get('download_size', 0.0)) proj.update_tiles = form.data.get('update_tiles', False) g.db.add( model.ImportRasterLayer(source=form.raster_source.data, start_level=int(form.start_level.data), end_level=int(form.end_level.data), project=proj)) g.db.commit() if form.start.data == 'start': redirect_url = url_for('project.start_raster_import', id=proj.id) return redirect(redirect_url) elif request.method == 'POST': flash(_('form error'), 'error') form.title.data = proj.title if proj.import_raster_layers: form.raster_source.data = proj.import_raster_layers[0].source if not request.form.get('start_level'): form.start_level.data = proj.import_raster_layers[0].start_level if not request.form.get('end_level'): form.end_level.data = proj.import_raster_layers[0].end_level coverage = proj.coverage if proj.coverage else 'null' if form.coverage.data: coverage = form.coverage.data base_layers = g.db.query( model.ExternalWMTSSource).filter_by(background_layer=True).all() for base_layer in base_layers: base_layer.bbox = base_layer.bbox_from_view_coverage() free_disk_space = diskspace_available_in_mb( current_app.config.geobox_state.user_data_path()) couch_url = 'http://%s:%s' % ('127.0.0.1', current_app.config.geobox_state.config.get( 'couchdb', 'port')) couch_layers = list(vector_layers_metadata(couch_url)) couchlayers_form = forms.SelectCouchLayers() couch_titles = [] for key, couch in enumerate(couch_layers): couch_titles.append((key, couch['title'])) couchlayers_form.select_couch.choices = couch_titles return render_template('projects/import_edit.html', proj=proj, form=form, sources=sources, couch_layers=couch_layers, base_layers=base_layers, coverage_form=coverage_form, couchlayers_form=couchlayers_form, coverage=coverage, free_disk_space=free_disk_space, with_server=True)
def export_edit(id=None): if id is None: proj = model.ExportProject() else: proj = g.db.query(model.ExportProject).get(id) if not proj: abort(404) raster_sources = g.db.query(model.LocalWMTSSource).order_by( model.LocalWMTSSource.id).all() coverage_form = forms.SelectCoverage() form = forms.ExportProjectEdit(request.form) form.end_level.choices = form.start_level.choices = get_levels( model.LocalWMTSSource) form.srs.choices = list( current_app.config.geobox_state.config.get('web', 'available_srs')) if form.validate_on_submit(): proj.title = form.data['title'] proj.export_raster_layers = [] proj.export_vector_layers = [] if form.coverage.data != 'false': proj.coverage = prepare_feature_collection_coverage( form.coverage.data) else: proj.coverage = '' proj.export_srs = form.srs.data proj.export_format = form.format.data proj.download_size = float(form.data.get('download_size', 0.0)) g.db.commit() redirect_url = url_for('.export_list', id=id) for raster_layer in json.loads(form.data['raster_layers']): raster_source = g.db.query(model.LocalWMTSSource).get( raster_layer['source_id']) start_level = int(raster_layer['start_level']) end_level = int( raster_layer.get('end_level') or raster_layer['start_level']) if not proj.coverage: raster_coverage = raster_source.wmts_source.download_coverage proj.coverage = raster_coverage else: raster_coverage = proj.coverage g.db.add( model.ExportRasterLayer(source=raster_source, start_level=start_level, end_level=end_level, project=proj)) g.db.commit() if form.start.data == 'start': create_export_tasks(proj) redirect_url = url_for('tasks.list') return redirect(redirect_url) elif request.method == 'POST': flash(_('form error'), 'error') form.title.data = proj.title form.format.data = proj.export_format form.srs.data = proj.export_srs coverage = proj.coverage if proj.coverage else 'null' if form.coverage.data: coverage = form.coverage.data base_layers = g.db.query( model.ExternalWMTSSource).filter_by(background_layer=True).all() for base_layer in base_layers: base_layer.bbox = base_layer.bbox_from_view_coverage() free_disk_space = diskspace_available_in_mb( current_app.config.geobox_state.user_data_path()) cache_url = get_external_couch_url(request) couch_url = 'http://%s:%s' % ('127.0.0.1', current_app.config.geobox_state.config.get( 'couchdb', 'port')) couch_layers = list(vector_layers_metadata(couch_url)) couchlayers_form = forms.SelectCouchLayers() couch_titles = [] for key, couch in enumerate(couch_layers): couch_titles.append((key, couch['title'])) couchlayers_form.select_couch.choices = couch_titles return render_template('projects/export_edit.html', proj=proj, form=form, raster_sources=raster_sources, couch_layers=couch_layers, layers=proj.export_raster_layers, base_layers=base_layers, coverage=coverage, free_disk_space=free_disk_space, coverage_form=coverage_form, couchlayers_form=couchlayers_form, cache_url=cache_url)
def import_edit(id=None): sources = g.db.query(model.ExternalWMTSSource).all() if id is None: proj = model.ImportProject() else: proj = g.db.query(model.ImportProject).get(id) if not proj: abort(404) coverage_form = forms.SelectCoverage() form = forms.ImportProjectEdit(request.form) form.start_level.choices = get_levels(model.ExternalWMTSSource) form.end_level.choices = get_levels(model.ExternalWMTSSource) form.update_tiles.checked = proj.update_tiles if form.validate_on_submit() and validate_max_tiles(form): redirect_url = url_for('.import_list', id=id) proj.title = form.data['title'] proj.import_raster_layers = [] if form.coverage.data != 'false': proj.coverage = prepare_feature_collection_coverage(form.coverage.data) else: proj.coverage = '' coverage = None proj.download_size = float(form.data.get('download_size', 0.0)) proj.update_tiles = form.data.get('update_tiles', False) g.db.add(model.ImportRasterLayer( source=form.raster_source.data, start_level=int(form.start_level.data), end_level=int(form.end_level.data), project=proj )) g.db.commit() if form.start.data == 'start': redirect_url = url_for('project.start_raster_import', id=proj.id) return redirect(redirect_url) elif request.method == 'POST': flash(_('form error'), 'error') form.title.data = proj.title if proj.import_raster_layers: form.raster_source.data = proj.import_raster_layers[0].source if not request.form.get('start_level'): form.start_level.data = proj.import_raster_layers[0].start_level if not request.form.get('end_level'): form.end_level.data = proj.import_raster_layers[0].end_level coverage = proj.coverage if proj.coverage else 'null' if form.coverage.data: coverage = form.coverage.data base_layers = g.db.query(model.ExternalWMTSSource).filter_by( background_layer=True).all() for base_layer in base_layers: base_layer.bbox = base_layer.bbox_from_view_coverage() free_disk_space = diskspace_available_in_mb( current_app.config.geobox_state.user_data_path()) couch_url = 'http://%s:%s' % ( '127.0.0.1', current_app.config.geobox_state.config.get('couchdb', 'port') ) couch_layers = list(vector_layers_metadata(couch_url)) couchlayers_form = forms.SelectCouchLayers() couch_titles = [] for key, couch in enumerate(couch_layers): couch_titles.append((key, couch['title'])) couchlayers_form.select_couch.choices = couch_titles return render_template('projects/import_edit.html', proj=proj, form=form, sources=sources, couch_layers=couch_layers, base_layers=base_layers, coverage_form=coverage_form, couchlayers_form=couchlayers_form, coverage=coverage, free_disk_space=free_disk_space, with_server=True)
def export_edit(id=None): if id is None: proj = model.ExportProject() else: proj = g.db.query(model.ExportProject).get(id) if not proj: abort(404) raster_sources = g.db.query(model.LocalWMTSSource).order_by(model.LocalWMTSSource.id).all() coverage_form = forms.SelectCoverage() form = forms.ExportProjectEdit(request.form) form.end_level.choices = form.start_level.choices = get_levels(model.LocalWMTSSource) form.srs.choices = list(current_app.config.geobox_state.config.get('web', 'available_srs')) if form.validate_on_submit(): proj.title = form.data['title'] proj.export_raster_layers = [] proj.export_vector_layers = [] if form.coverage.data != 'false': proj.coverage = prepare_feature_collection_coverage(form.coverage.data) else: proj.coverage = '' proj.export_srs = form.srs.data proj.export_format = form.format.data proj.download_size = float(form.data.get('download_size', 0.0)) g.db.commit() redirect_url = url_for('.export_list', id=id) for raster_layer in json.loads(form.data['raster_layers']): raster_source = g.db.query(model.LocalWMTSSource).get(raster_layer['source_id']) start_level = int(raster_layer['start_level']) end_level = int(raster_layer.get('end_level') or raster_layer['start_level']) if not proj.coverage: raster_coverage = raster_source.wmts_source.download_coverage proj.coverage = raster_coverage else: raster_coverage = proj.coverage g.db.add(model.ExportRasterLayer( source=raster_source, start_level=start_level, end_level=end_level, project=proj )) g.db.commit() if form.start.data == 'start': create_export_tasks(proj) redirect_url = url_for('tasks.list') return redirect(redirect_url) elif request.method == 'POST': flash(_('form error'), 'error') form.title.data = proj.title form.format.data = proj.export_format form.srs.data = proj.export_srs coverage = proj.coverage if proj.coverage else 'null' if form.coverage.data: coverage = form.coverage.data base_layers = g.db.query(model.ExternalWMTSSource).filter_by(background_layer=True).all() for base_layer in base_layers: base_layer.bbox = base_layer.bbox_from_view_coverage() free_disk_space = diskspace_available_in_mb(current_app.config.geobox_state.user_data_path()) cache_url = get_external_couch_url(request) couch_url = 'http://%s:%s' % ('127.0.0.1', current_app.config.geobox_state.config.get('couchdb', 'port')) couch_layers = list(vector_layers_metadata(couch_url)) couchlayers_form = forms.SelectCouchLayers() couch_titles = [] for key, couch in enumerate(couch_layers): couch_titles.append((key, couch['title'])) couchlayers_form.select_couch.choices = couch_titles return render_template('projects/export_edit.html', proj=proj, form=form, raster_sources=raster_sources,couch_layers=couch_layers, layers=proj.export_raster_layers, base_layers=base_layers, coverage=coverage, free_disk_space=free_disk_space,coverage_form=coverage_form, couchlayers_form=couchlayers_form, cache_url=cache_url)
def import_vector(): from ...lib.vectormapping import Mapping uploaded_shapes = [] upload_failures = [] not_allowed_files = [] for upload_file in request.files.getlist('file_upload'): import_dir = current_app.config.geobox_state.user_data_path('import') target = os.path.join(import_dir, upload_file.filename) upload_file_ext = os.path.splitext(target)[1].lower() if upload_file_ext in ('.shp', '.shx', '.dbf'): try: if not os.path.isdir(import_dir): os.mkdir(import_dir) f = open(target, 'wb') f.write(upload_file.stream.read()) f.close() except IOError: upload_failures.append(upload_file.filename) uploaded_shapes.append(upload_file.filename) elif upload_file_ext in ('.sbn', '.sbx', '.prj', '.index', '.cpg'): # ignore files without showing message continue else: not_allowed_files.append(upload_file.filename) form = forms.ImportVectorEdit(request.form) form.srs.choices = list(current_app.config.geobox_state.config.get('web', 'available_srs')) form.srs.choices.insert(0, ('', _('-- select srs --'), '')) couch_url = 'http://%s:%s' % ('127.0.0.1', current_app.config.geobox_state.config.get('couchdb', 'port')) form.layers.choices = [(item['dbname'], item['title']) for item in vector_layers_metadata(couch_url)] form.layers.choices.insert(0, ('', _('-- select layer or add new --'))) shape_files, missing_files = get_shapefile_list() form.file_name.choices = [(name, name) for name in shape_files] form.file_name.choices.insert(0, ('', _('-- select a shapefile --'))) if not len(request.files): if form.validate_on_submit(): try: is_valid_shapefile(current_app.config.geobox_state.user_data_path('import', form.file_name.data), Mapping(None, None, '*')) except ConvertError as e: flash(e, 'error') return render_template('vector/import.html', form=form) except OSError: flash(_('invalid shapefile'), 'error') return render_template('vector/import.html', form=form) if (form.layers.data and form.name.data) or (not form.layers.data and not form.name.data): flash(_('please select new layer or current layer to import'), 'error') return redirect(url_for('.import_vector')) title = None if form.layers.data: layer = form.layers.data else: title = form.name.data layer = 'local_vector_' + re.sub(r'[^a-z0-9]*', '', title.lower()) if layer == 'local_vector_': flash(_('None of the characters used for the layer is allowed')) return redirect(url_for('.import_vector')) task = VectorImportTask( db_name=layer, title=title, file_name=form.file_name.data, srs=form.srs.data, type_='shp', ) g.db.add(task) g.db.commit() return redirect(url_for('tasks.list')) elif request.method == 'POST': flash(_('form error'), 'error') for missing_file in missing_files: flash(_('file %(name)s missing', name=missing_file), 'error') for upload_failure in upload_failures: flash(_('failed to upload %(name)s', name=upload_failure), 'error') for not_allowed_file in not_allowed_files: flash(_('filetype of %(name)s not allowed', name=not_allowed_file), 'error') for uploaded_shape in uploaded_shapes: flash(_('file %(name)s uploaded', name=uploaded_shape), 'info') file_browser = request_is_local() return render_template('vector/import.html', form=form, file_browser=file_browser)