Skip to content

Commit

Permalink
Add drag-and-drop of geoJSON objects
Browse files Browse the repository at this point in the history
  • Loading branch information
cavenel committed Oct 25, 2023
1 parent aaf9fc6 commit e76acc2
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 47 additions & 0 deletions tissuumaps/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(
[
Expand Down Expand Up @@ -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 == "":
Expand Down
11 changes: 11 additions & 0 deletions tissuumaps/static/js/flask.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions tissuumaps/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,10 +655,10 @@ def csvFile(completePath):
abort(404)


@app.route("/<path:completePath>.json")
@app.route("/<path:completePath>.<any(json, geojson, pbf):ext>")
@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):
Expand Down

0 comments on commit e76acc2

Please sign in to comment.