vingerha / gtfs2

Support GTFS in Home Assistant GUI-only
https://github.com/vingerha/gtfs2
MIT License
65 stars 4 forks source link

code suggestion: where clause for latitude/longitude... and the 180th meridian ...in progress.... #74

Closed FabienD74 closed 1 month ago

FabienD74 commented 1 month ago

we can simplify the SQL like this

        INNER JOIN stops stop
                   on stop.stop_id = st.stop_id and {latitude_longitude_where_clause}
        INNER JOIN routes route

using this:


#####################
#####################
# Latitude - WGS84 latitude in decimal degrees. The value must be greater than or equal to -90.0 and 
# less than or equal to 90.0.
# Example: 41.890169 for the Colosseum in Rome.
# Longitude - WGS84 longitude in decimal degrees. The value must be greater than or equal to -180.0 and 
# less than or equal to 180.0.
# Example: 12.492269 for the Colosseum in Rome.
#
# 180th meridian, "the antimeridian"  
# see wikipedia  https://en.wikipedia.org/wiki/180th_meridian
# TODO : try to find a fix/ workaround for people living around the "edge"
#  -181 ° => 179 °
#  181°   => -179 °
#  ... => we need to check 2 small areas,  on each side of the "antimeridian" :-))))
#

    area_min_longitude = 0
    area_min_latitude  = 0
    area_max_longitude = 0
    area_max_latitude = 0

    area_min_longitude = longitude - radius
    area_max_longitude = longitude + radius
    area_min_latitude = latitude - radius
    area_max_latitude = latitude + radius

    # north pole
    if area_max_latitude > 90:
        area_max_latitude = 90

    # south pole
    if area_min_latitude < -90:
        area_min_latitude = -90

    if area_max_longitude > 180:
        area_max_longitude = 180

    if area_min_longitude < -180:
        area_min_longitude = -180

    latitude_longitude_where_clause = f"""
        ( ( stop.stop_lat >={area_min_latitude}) and ( stop.stop_lat <={area_max_latitude})) and
        ( ( stop.stop_lon >={area_min_longitude}) and ( stop.stop_lon <={area_max_longitude}))
        """  # noqa: S608
#####################
#####################
vingerha commented 1 month ago

Ah...you mean to make the radius more precise ... I did not spend much time on that I agree...thanks for the suggestion, now it needs time, will keep it open till I have :)

vingerha commented 1 month ago

En + ... mon code n'est pas au top, j'ai commencé et ajouté et ajouté sans revoir mes changements

FabienD74 commented 1 month ago

Well in the mean time we check if stops are in a "square area" , then we could fine tune to make sure it in within the specified distance in meters... First things first...

PS: I went into this because i had to increase distance aver 1000 meters ( and change the code) to find the bus stop close to me.....

I'll check that afterward using googe map.... because i would like to sort "stops" around me by distance.... => new attributes for sensors i guess.... :-)

:-) Regards Fabien

vingerha commented 1 month ago

This is how I came to the conversion...used google maps and distance :) ... for my stops it was OK but I admit not having checked this in detail. I started with my location, then calculated the distance and then I came to the factor...and it is sort-of OK for a radius...all in all I was not sure how important this had to be

FabienD74 commented 1 month ago

We will have to make some shortcuts here and there...

La terre c'est 40.000 Km pour 360 degrés... 1km = 360/40.000 degrés 1m = 360/(40.000.000) degrés. ... oups pas grand chose quoi... à vérifier ...

vingerha commented 1 month ago

And.... you do have to verify if your provider has put the right lat/lon for the stops ... I am in the 06 where anything can happen...and so it does, a.o. my son uses a bus stop that does not show up anywhere LOL

FabienD74 commented 1 month ago

< joke on > J'sais pas ce qui peut se passer dans le 06, .... tu as Vladimir qui change les coordonées GSP ???? LOL ;-) ;-); < Joke off >

vingerha commented 1 month ago

Pas besoin des étrangers de plus (comme moi) pour faire des bétises

vingerha commented 1 month ago

Closing as added different calc as per your suggestion