diff --git a/examples/RasterLayer.ipynb b/examples/RasterLayer.ipynb index 2016fc0..991b336 100644 --- a/examples/RasterLayer.ipynb +++ b/examples/RasterLayer.ipynb @@ -13,7 +13,20 @@ "metadata": {}, "outputs": [], "source": [ - "from ipyopenlayers import Map, RasterTileLayer" + "from ipyopenlayers import Map, RasterTileLayer\n", + "import configparser\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "import configparser\n", + "config = configparser.ConfigParser()\n", + "config.read('../.env.ini')\n", + "api_key = config['DEFAULT']['api_key']" ] }, { @@ -26,7 +39,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "b55d411654314ab094b4b12ea0055e9f", + "model_id": "aef7ad2f21ec4cb9b7c3207b7158e5ea", "version_major": 2, "version_minor": 0 }, @@ -48,14 +61,33 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ - "import configparser\n", - "config = configparser.ConfigParser()\n", - "config.read('../.env.ini')\n", - "api_key = config['DEFAULT']['api_key']" + "import requests\n", + "import unicodedata\n", + "\n", + "def get_country_from_coordinates_geoapify(lat, lon):\n", + " config = configparser.ConfigParser()\n", + " config.read('../.env.ini')\n", + " api_key = config['DEFAULT']['api_key'] \n", + " url = f\"https://api.geoapify.com/v1/geocode/reverse?lat={lat}&lon={lon}&apiKey={api_key}\"\n", + " response = requests.get(url)\n", + " data = response.json()\n", + " features = data.get('features', [])\n", + "\n", + " if features:\n", + " first_feature = features[0]\n", + " properties = first_feature.get('properties', {})\n", + " country = properties.get('country', None)\n", + " normalized_name = country.split(' ')[0]\n", + " normalized_name = unicodedata.normalize('NFKD', normalized_name)\n", + " normalized_name = normalized_name.encode('ASCII', 'ignore').decode('utf-8')\n", + "\n", + " print(normalized_name)\n", + "\n", + "m.on_click(get_country_from_coordinates_geoapify)\n" ] }, { diff --git a/ipyopenlayers/Map.py b/ipyopenlayers/Map.py index 1979c0e..0698647 100644 --- a/ipyopenlayers/Map.py +++ b/ipyopenlayers/Map.py @@ -144,7 +144,7 @@ class Map(DOMWidget): def __init__(self, center=None, zoom=None, **kwargs): super().__init__(**kwargs) - + self._click_callbacks = [] if center is not None: self.center = center if zoom is not None: @@ -183,60 +183,43 @@ def _handle_msg(self, msg): buffers = msg.get('buffers', []) self._msg_callback(self, content, buffers) - def _handle_click(self, lat, lon, api_key): - """ - Handle click events and trigger registered callbacks. - """ - print(f"Handling click event at Longitude: {lon}, Latitude: {lat}") - country = self.get_country_from_coordinates_geoapify(lat, lon, api_key) - self.clicked_country = country - print(f"Country: {country}") + def _handle_click(self, lat, lon): + """Handle click events and trigger registered callbacks.""" - callbacks = self._click_callbacks.get_callbacks() if self._click_callbacks else [] + + callbacks = self._click_callbacks.get_callbacks() for callback in callbacks: if callable(callback): - callback(lat, lon, country) + callback(lat, lon) - def on_click(self, callback, api_key): + def on_click(self, callback): """ - Register a callback to handle click events on the map and pass the API key. + Register a callback to handle click events on the map. + + + Parameters + ---------- + callback : function + Function that accepts two arguments: lon (longitude) and lat (latitude) of the clicked position. """ + self._click_callback = callback - + + def handle_frontend_event(widget, content, buffers): """Handle the click event from the frontend.""" data = content.get('data', {}) method = data.get('method', '') + if method == 'custom': event_data = data.get('content', {}) lon = event_data.get('lon') lat = event_data.get('lat') - + if callable(self._click_callback): - self._click_callback(lat, lon, self.get_country_from_coordinates_geoapify(lat, lon, api_key)) + self._click_callback(lat, lon) self.on_msg(handle_frontend_event) - - def normalize_country_name(self, country_name): - normalized_name = country_name.split(' ')[0] - normalized_name = unicodedata.normalize('NFKD', normalized_name) - normalized_name = normalized_name.encode('ASCII', 'ignore').decode('utf-8') - return normalized_name - - def get_country_from_coordinates_geoapify(self, lat, lon, api_key): - url = f"https://api.geoapify.com/v1/geocode/reverse?lat={lat}&lon={lon}&apiKey={api_key}" - response = requests.get(url) - data = response.json() - features = data.get('features', []) - - if features: - first_feature = features[0] - properties = first_feature.get('properties', {}) - country = properties.get('country', None) - if country: - normalized_name = country.split(' ')[0] - normalized_name = unicodedata.normalize('NFKD', normalized_name) - normalized_name = normalized_name.encode('ASCII', 'ignore').decode('utf-8') - return normalized_name - return "Unknown" \ No newline at end of file + +