diff --git a/CHANGELOG.md b/CHANGELOG.md index 2595f3f0..b816abff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ - Move to WebGL rendering for regions - Refactor completely the region tab to better handle large number of items - Allow import of regions from multiple GeoJSON files +- Add support for pbf region files +- Add drag-and-drop opening of GeoJSON files - Add histogram button for analyzing individual regions - Make regions work in collection mode and with multiple image layers - Add marker outline and fill options in Advanced options diff --git a/tissuumaps/gui.py b/tissuumaps/gui.py index 4f301f0d..4c3a1fba 100644 --- a/tissuumaps/gui.py +++ b/tissuumaps/gui.py @@ -530,6 +530,8 @@ def dropEvent(self, event): self.openImagePath(link) elif file_extension == ".csv": self.page().runJavaScript(f'flask.standalone.addCSV("{link}");') + elif file_extension in [".json", ".geojson", ".pbf"]: + self.page().runJavaScript(f'flask.standalone.addGeoJSON("{link}");') else: self.page().runJavaScript(f'flask.standalone.addLayer("{link}");') # self.emit(SIGNAL("dropped"), links) @@ -714,6 +716,18 @@ def openImagePath(self, folderpath): ) self.page().runJavaScript(f'flask.standalone.addCSV("{folderpath}");') return True + elif file_extension in [".json", ".geojson", ".pbf"]: + logging.debug( + " ".join( + [ + "Opening json:", + str(self.app.basedir), + str(self.location + imgPath), + ] + ) + ) + self.page().runJavaScript(f'flask.standalone.addGeoJSON("{folderpath}");') + return True logging.debug( " ".join( [ @@ -775,6 +789,39 @@ def addCSV(self, path, csvpath): } return returnDict + @Slot(str, str, result="QJsonObject") + def addGeoJSON(self, path, geoJSONpath): + if geoJSONpath == "": + geoJSONpath = QFileDialog.getOpenFileName(self, "Select a File")[0] + if not geoJSONpath: + returnDict = {"geoJSONPath": None} + return returnDict + parts = Path(geoJSONpath).parts + if parts[0] == "https:": + imgPath = parts[-1] + relativePath = "/".join(parts[:-1]) + + else: + if self.app.basedir != parts[0]: + if not self.app.basedir == os.path.abspath( + self.app.config["SLIDE_DIR"] + ): + QMessageBox.warning( + self, "Error", "All files must be in the same drive." + ) + returnDict = {"geoJSONPath": None} + return returnDict + else: + self.app.basedir = parts[0] + imgPath = os.path.join(*parts[1:]) + + path = os.path.abspath(os.path.join(self.app.basedir, path)) + imgPath = os.path.abspath(os.path.join(self.app.basedir, imgPath)) + + relativePath = os.path.relpath(os.path.dirname(imgPath), path) + returnDict = {"geoJSONPath": relativePath + "/" + os.path.basename(imgPath)} + return returnDict + @Slot(str, str, result="QJsonObject") def addLayer(self, path, layerpath): if layerpath == "": diff --git a/tissuumaps/static/js/flask.js b/tissuumaps/static/js/flask.js index 33f9d05c..8b8b45be 100644 --- a/tissuumaps/static/js/flask.js +++ b/tissuumaps/static/js/flask.js @@ -94,6 +94,17 @@ flask.standalone.init = function () { }, true); }; +flask.standalone.addGeoJSON = function (filename) { + const queryString = window.location.search; + const urlParams = new URLSearchParams(queryString); + const path = urlParams.get('path') + flask.standalone.backend.addGeoJSON(path, filename, function(geoJSONJSON) { + if (geoJSONJSON["geoJSONPath"]!=null) { + regionUtils.JSONToRegions(geoJSONJSON["geoJSONPath"]); + } + }); +} + flask.standalone.addCSV = function (filename) { const queryString = window.location.search; const urlParams = new URLSearchParams(queryString); diff --git a/tissuumaps/views.py b/tissuumaps/views.py index 24e30b45..8497b7a6 100644 --- a/tissuumaps/views.py +++ b/tissuumaps/views.py @@ -655,10 +655,10 @@ def csvFile(completePath): abort(404) -@app.route("/.json") +@app.route("/.") @requires_auth -def jsonFile(completePath): - completePath = os.path.join(app.basedir, completePath + ".json") +def jsonFile(completePath, ext): + completePath = os.path.join(app.basedir, completePath + "." + ext) directory = os.path.dirname(completePath) filename = os.path.basename(completePath) if os.path.isfile(completePath):