def import_to_db(uid): """Function to import features from a layer of a shapefile into the database and calculation of the concavehull of the features in the table to use as a bounding polygon for the OverpassAPI request. GET request: The import site is returned, containing a set of the uploaded shapefiles. The user can then choose the shapefile to import. POST request: The chosen layer will be imported into a new table using the the function layer_to_db from the OSMDeviationfinder class. This function will import the features and convert multigeometry features to single geometry features. After a successful import, the concavehull of the imported data is generated using the function get_concavehull of the OSMDeviationfinder class. The concavhull is saved for the current devmap in the xy (for the OverpassAPI) and yx (for leaflet.js) representation. After that, the osm data download site is returned. """ error = None fdir = os.path.join(app.config["UPLOAD_FOLDER"], uid) fdata = dict() if request.method == "POST": uid = uid.encode("ISO-8859-1") fdata["datasource"] = request.form["source"] fdata["title"] = request.form["title"] fdata["datalicense"] = request.form["license"] fdata["shapefile"] = request.form["shapefile"] fdata["wmsformat"] = request.form["wmsformat"] fdata["wmsurl"] = request.form["wmsurl"] fdata["wmslayer"] = request.form["wmslayer"] if len(fdata["datasource"]) < 4: error = "Please define a data source with at least 4 characters." if len(fdata["datalicense"]) < 3: error = "Please a license with at least 2 characters." if len(fdata["title"]) < 4: error = "Please define a title with at least 4 characters." if len(fdata["wmsurl"]) > 1 or len(fdata["wmslayer"]) > 1 or len(fdata["wmsformat"]) > 1: if not (fdata["wmsurl"]) > 12 and len(fdata["wmslayer"]) > 3 and len(fdata["wmsformat"]) > 12: error = "All fields for a custom WMS Basemap have to be filled." if not "image" in fdata["wmsformat"]: error = "Please define a correct image format eg. image/jpeg" else: dm = DevMap.query.filter_by(title=fdata["title"]).first() if dm and dm.uid != uid: error = 'The title "' + fdata["title"] + '" is already chosen. Please try another title.' if fdata["shapefile"] == "No Shapefile found!": error = "No shapefile was found." if error is None: f = os.path.join(fdir, fdata["shapefile"]) tablename = "odf_" + uid + "_ref" shapefile = ogr.Open(f) devfinder = OSMDeviationfinder(connectioninfo) s = shapefile.GetLayerByIndex(0) devfinder.layer_to_db(s, tablename, True) concavehull = devfinder.get_concavehull(tablename) dm = DevMap.query.filter_by(uid=uid).first() if ( current_user.is_authenticated() and dm.owner == current_user or dm.owner == User.query.filter_by(username="******").first() ): boundsyx = { "type": "Feature", "properties": { "uid": uid, "title": fdata["title"], "author": dm.owner.username, "source": fdata["datasource"], }, "geometry": {"type": "Polygon", "coordinates": [concavehull[1]["coordinates"][0]]}, } boundsxy = { "type": "Feature", "properties": { "uid": uid, "title": fdata["title"], "author": dm.owner.username, "source": fdata["datasource"], }, "geometry": {"type": "Polygon", "coordinates": [concavehull[0]["coordinates"][0]]}, } dm.boundsxy = boundsxy dm.boundsyx = boundsyx dm.datasource = fdata["datasource"] dm.title = fdata["title"] dm.datalicense = fdata["datalicense"] dm.basemapwmsurl = fdata["wmsurl"] dm.basemapwmslayer = fdata["wmslayer"] dm.basemapwmsformat = fdata["wmsformat"] db.session.add(dm) db.session.commit() return redirect(url_for("devmap.osm_download", uid=uid)) shapefiles = [] for f in os.listdir(fdir): if f.endswith(".shp") and not f.startswith("."): s = Shapefile(f, None, fdir) shapefiles.append(s) return render_template("import.html", shapefiles=shapefiles, uid=uid, error=error, fdata=fdata)