How to get a time zone from a location using latitude and longitude coordinates?

后端 未结 17 1644
走了就别回头了
走了就别回头了 2020-11-21 04:38

Given the latitude and longitude of a location, how does one know what time zone is in effect in that location?

In most cases, we are looking for an IANA/Olson time z

17条回答
  •  旧时难觅i
    2020-11-21 05:06

    1. There are several sources online that have geojson data for timezones (here's one, here's another)

    2. Use a geometry library to create polygon objects from the geojson coordinates (shapely [python], GEOS [c++], JTS [java], NTS [.net]).

    3. Convert your lat/lng to a point object (however your library represents that) and check if it intersects the timezone polygon.

      from shapely.geometry import Polygon, Point
      
      def get_tz_from_lat_lng(lat, lng):
          for tz, geojson in timezones.iteritems():
              coordinates = geojson['features'][0]['geometry']['coordinates']
              polygon = Polygon(coordinates)
              point = Point(lng, lat)
              if polygon.contains(point):
                  return tz
      

提交回复
热议问题