More general location representation

main
Bob Mottram 2023-08-03 13:35:43 +01:00
parent 30de40ed0b
commit dd5a353c95
4 changed files with 51 additions and 13 deletions

View File

@ -150,7 +150,7 @@ from content import valid_url_lengths
from content import remove_script
from threads import begin_thread
from maps import get_map_links_from_post_content
from maps import get_location_from_tags
from maps import get_location_from_post
from maps import add_tag_map_links
from maps import geocoords_from_map_link
@ -355,8 +355,7 @@ def store_hash_tags(base_dir: str, nickname: str, domain: str,
post_content = post_json_object['object']['content']
map_links += get_map_links_from_post_content(post_content)
# get geolocation from tags
location_str = \
get_location_from_tags(post_json_object['object']['tag'])
location_str = get_location_from_post(post_json_object)
if location_str:
if '://' in location_str and '.' in location_str:
zoom, latitude, longitude = geocoords_from_map_link(location_str)

49
maps.py
View File

@ -16,9 +16,18 @@ from utils import load_json
from utils import save_json
from utils import locate_post
from utils import remove_html
from utils import has_object_dict
def get_location_from_tags(tags: []) -> str:
def _geocoords_to_osm_link(osm_domain: str, zoom: int,
latitude: float, longitude: float) -> str:
"""Returns an OSM link for the given geocoordinates
"""
return 'https://www.' + osm_domain + '/#map=' + \
str(zoom) + '/' + str(latitude) + '/' + str(longitude)
def _get_location_from_tags(tags: []) -> str:
"""Returns the location from the tags list
"""
for tag_item in tags:
@ -35,6 +44,39 @@ def get_location_from_tags(tags: []) -> str:
return None
def get_location_from_post(post_json_object: {}) -> str:
"""Returns the location for the given post
"""
locn = None
# location represented via a tag
post_obj = post_json_object
if has_object_dict(post_json_object):
post_obj = post_json_object['object']
if post_obj.get('tag'):
if isinstance(post_obj['tag'], list):
locn = _get_location_from_tags(post_obj['tag'])
# location representation used by pixelfed
locn_exists = False
if post_obj.get('location'):
if isinstance(post_obj['location'], dict):
if post_obj['location'].get('longitude') and \
post_obj['location'].get('latitude'):
if isinstance(post_obj['location']['longitude'], str) and \
isinstance(post_obj['location']['latitude'], str):
if is_float(post_obj['location']['longitude']) and \
is_float(post_obj['location']['latitude']):
locn_exists = True
if locn_exists:
osm_domain = 'osm.org'
zoom = 17
locn = _geocoords_to_osm_link(osm_domain, zoom,
post_obj['location']['latitude'],
post_obj['location']['longitude'])
return locn
def _geocoords_from_osm_link(url: str, osm_domain: str) -> (int, float, float):
"""Returns geocoordinates from an OSM map link
"""
@ -298,7 +340,7 @@ def html_open_street_map(url: str,
return ''
if not zoom:
return ''
osm_url = _geocoords_to_osm_link(osm_domain, zoom, latitude, longitude)
html_str = \
'<iframe width="' + width + '" height="' + height + \
'" frameborder="0" ' + \
@ -312,8 +354,7 @@ def html_open_street_map(url: str,
'%2C' + \
str(latitude + bounding_box_degrees) + \
'&amp;layer=mapnik" style="border: 1px solid black"></iframe>' + \
'<br/><small><a href="https://www.' + osm_domain + '/#map=' + \
str(zoom) + '/' + str(latitude) + '/' + str(longitude) + \
'<br/><small><a href="' + osm_url + \
'">' + translate['View Larger Map'] + '</a></small>\n'
return html_str

View File

@ -38,7 +38,7 @@ from webapp_utils import edit_currency_field
from webapp_post import individual_post_as_html
from maps import get_map_preferences_url
from maps import get_map_preferences_coords
from maps import get_location_from_tags
from maps import get_location_from_post
def _html_new_post_drop_down(scope_icon: str, scope_description: str,
@ -305,8 +305,7 @@ def html_new_post(edit_post_params: {},
if edited_post_json['object'].get('tag'):
# if this is an edited post then get the location
location_str = \
get_location_from_tags(edited_post_json['object']['tag'])
location_str = get_location_from_post(edited_post_json)
if location_str:
default_location = location_str
# if this is an edited post then get the start and end time

View File

@ -108,7 +108,7 @@ from maps import html_open_street_map
from maps import set_map_preferences_coords
from maps import set_map_preferences_url
from maps import geocoords_from_map_link
from maps import get_location_from_tags
from maps import get_location_from_post
from session import get_json
@ -2768,8 +2768,7 @@ def individual_post_as_html(signing_priv_key_pem: str,
'content', False, True)
buy_links = get_buy_links(post_json_object, translate, buy_sites)
# show embedded map if the location contains a map url
location_str = \
get_location_from_tags(post_json_object['object']['tag'])
location_str = get_location_from_post(post_json_object)
if location_str:
if '://' in location_str and '.' in location_str:
bounding_box_degrees = 0.001