Skip to content

Commit

Permalink
changes onclick
Browse files Browse the repository at this point in the history
  • Loading branch information
Nour-Cheour10 committed Aug 26, 2024
1 parent 052c71f commit 244e5f4
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 46 deletions.
46 changes: 39 additions & 7 deletions examples/RasterLayer.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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']"
]
},
{
Expand All @@ -26,7 +39,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "b55d411654314ab094b4b12ea0055e9f",
"model_id": "aef7ad2f21ec4cb9b7c3207b7158e5ea",
"version_major": 2,
"version_minor": 0
},
Expand All @@ -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"
]
},
{
Expand Down
61 changes: 22 additions & 39 deletions ipyopenlayers/Map.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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"


0 comments on commit 244e5f4

Please sign in to comment.