Skip to content

Commit

Permalink
Fix #521 wrong coordinates send to JOSM
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustry committed Jan 17, 2025
1 parent b20051e commit 105ad40
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 35 deletions.
9 changes: 2 additions & 7 deletions QuickOSM/core/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from qgis.utils import OverrideCursor, iface, plugins

from QuickOSM.core import process
from QuickOSM.core.josm_remote import open_object
from QuickOSM.core.utilities.utilities_qgis import open_webpage
from QuickOSM.definitions.action import Visibility
from QuickOSM.qgis_plugin_tools.tools.i18n import tr
Expand Down Expand Up @@ -195,13 +196,7 @@ def run(field: str, value: str):
open_webpage(url)

elif field == 'josm':
import urllib.error
import urllib.parse
import urllib.request
try:
url = 'http://localhost:8111/load_object?objects=' + value
urllib.request.urlopen(url).read()
except urllib.error.URLError:
if not open_object(value):
iface.messageBar().pushMessage(
tr('The JOSM remote seems to be disabled.'),
level=Qgis.Critical,
Expand Down
46 changes: 46 additions & 0 deletions QuickOSM/core/josm_remote.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from qgis.core import (
QgsCoordinateReferenceSystem,
QgsCoordinateTransform,
QgsNetworkAccessManager,
QgsProject,
QgsRectangle,
)
from qgis.PyQt.QtCore import QUrl
from qgis.PyQt.QtNetwork import QNetworkReply, QNetworkRequest

__copyright__ = 'Copyright 2025, 3Liz'
__license__ = 'GPL version 3'
__email__ = '[email protected]'

URL = "http://localhost:8111"


def open_object(object_id: str) -> bool:
""" Open the given OSM object in JOSM. """
return _josm_request(f"load_object?objects={object_id}")


def open_extent(extent: QgsRectangle, crs_map: QgsCoordinateReferenceSystem) -> bool:
""" Open the given extent in JOSM. """
if crs_map.authid() != 'EPSG:4326':
crs_4326 = QgsCoordinateReferenceSystem("EPSG:4326")
transform = QgsCoordinateTransform(crs_map, crs_4326, QgsProject.instance())
extent = transform.transform(extent)

url = (
f'load_and_zoom?'
f'left={extent.xMinimum()}&right={extent.xMaximum()}&'
f'top={extent.yMaximum()}&bottom={extent.yMinimum()}'
)

return _josm_request(url)


def _josm_request(request_url: str) -> bool:
""" Do the request to JOSM. """
request = QNetworkRequest()
# noinspection PyArgumentList
request.setUrl(QUrl(f"{URL}/{request_url}"))
# noinspection PyArgumentList
reply: QNetworkReply = QgsNetworkAccessManager.instance().get(request)
return reply.error() == QNetworkReply.NetworkError.NoError
33 changes: 5 additions & 28 deletions QuickOSM/quick_osm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import logging
import os
import shutil
import urllib.request

from os.path import join

Expand All @@ -25,6 +24,7 @@
QPushButton,
)

from QuickOSM.core.josm_remote import open_extent
from QuickOSM.core.utilities.tools import (
check_processing_enable,
get_setting,
Expand Down Expand Up @@ -204,33 +204,10 @@ def show_help():
def josm_remote(self):
"""Call the JOSM remote control using the current canvas extent."""
map_settings = self.iface.mapCanvas().mapSettings()
extent = map_settings.extent()
crs_map = map_settings.destinationCrs()
if crs_map.authid() != 'EPSG:4326':
crs_4326 = QgsCoordinateReferenceSystem(4326)
transform = QgsCoordinateTransform(
crs_map, crs_4326, QgsProject.instance())
extent = transform.transform(extent)

url = (
f'http://localhost:8111/load_and_zoom?'
f'left={extent.xMinimum()}&right={extent.xMaximum()}&'
f'top={extent.xMaximum()}&bottom={extent.yMinimum()}'
)
try:
request = urllib.request.Request(url)
result_request = urllib.request.urlopen(request)
result = result_request.read()
result = result.decode('utf8')
if result.strip().upper() != 'OK':
self.iface.messageBar().pushCritical(
tr('JOSM Remote'), result)
else:
self.iface.messageBar().pushSuccess(
tr('JOSM Remote'), tr('Import done, check JOSM.'))
except OSError:
self.iface.messageBar().pushCritical(
tr('JOSM Remote'), tr('Is the remote enabled in the JOSM settings?'))
if open_extent(map_settings.extent(), map_settings.destinationCrs()):
self.iface.messageBar().pushSuccess(tr('JOSM Remote'), tr('Import done, check JOSM.'))
else:
self.iface.messageBar().pushCritical(tr('JOSM Remote'), tr('Is the remote enabled in the JOSM settings?'))

def open_dialog(self):
"""Create and open the main dialog."""
Expand Down

0 comments on commit 105ad40

Please sign in to comment.