Determine timezone from latitude/longitude without

2019-01-03 23:21发布

is there any possibility to determine the timezone of point (lat/lon) without using webservices? Geonames.org is not stable enough for me to use :( I need this to work in PHP.

Thanks

16条回答
成全新的幸福
2楼-- · 2019-01-03 23:43

I downloaded data that matches 5 digit zip codes to time zones, and data that determines the UTC offset for each time zone during DST and non-DST periods.

Then I simplified the data to only consider the first three digits of the ZIP Code, since all ZIP codes that share the first three digits are very close to each other; the three digits identify a unique mail distribution center.

The resulting Json file does require you to decide whether or not you are subject to DST currently, and it probably has some inaccuracy here and there. But it's a pretty good local solution that is very compact and simple to query.

Here it is: https://gist.github.com/benjiwheeler/8aced8dac396c2191cf0

查看更多
一夜七次
3楼-- · 2019-01-03 23:44

I ran into this problem while working on another project and looked into it very deeply. I found all of the existing solutions to be lacking in major ways.

Downloading the GeoNames data and using some spatial index to look up the nearest point is definitely an option, and it will yield the correct result a lot of the time, but it can easily fail if a query point is on the wrong side of a time zone border from the nearest point in the database.

A more accurate method is to use a digital map of the time zones and to write code to find the polygon in that map that contains a given query point. Thankfully, there is an excellent map of the time zones of the world available at http://efele.net/maps/tz/world/. To write an efficient query engine, you need to:

  • Parse the ESRI shapefile format into a useful internal representation.
  • Write point-in-polygon code to test whether a given query point is in a given polygon.
  • Write an efficient spatial index on top of the polygon data so that you don't need to check every polygon to find the containing one.
  • Handle queries that are not contained by any polygon (e.g., in the ocean). In such cases, you should "snap to" the nearest polygon up to a certain distance, and revert to the "natural" time zone (the one determined by longitude alone) in the open ocean. To do this, you will need code to compute the distance between a query point and a line segment of a polygon (this is non-trivial since latitude and longitude are a non-Euclidean coordinate system), and your spatial index will need to be able to return nearby polygons, not just potentially containing polygons.

Each of those are worthy of their own Stack Overflow question/answer page.

After concluding that none of the existing solutions out there met my needs, I wrote my own solution and made it available here:

http://askgeo.com

AskGeo uses a digital map and has a highly optimized spatial index that allows for running more than 10,000 queries per second on my computer in a single thread. And it is thread safe, so even higher throughput is certainly possible. This is a serious piece of code, and it took us a long time to develop, so we are offering it under a commercial license.

It is written in Java, so using it in PHP would involve using:

http://php-java-bridge.sourceforge.net/doc/how_it_works.php

We are also open to porting it for a bounty. For details on the pricing, and for detailed documentation, see http://askgeo.com.

I hope this is useful. It certainly was useful for the project I was working on.

查看更多
SAY GOODBYE
4楼-- · 2019-01-03 23:47

I had this problem a while back and did exactly what adam suggested:

  • Download the database of cities from geonames.org
  • convert it to a compact lat/lon -> timezone list
  • use an R-Tree implementation to efficiently lookup the nearest city (or rather, its timezone) to a given coordinate

IIRC it took less than 1 second to populate the R-Tree, and it could then perform thousands of lookups per second (both on a 5 year old PC).

查看更多
We Are One
5楼-- · 2019-01-03 23:48

Another solution is to import a table of cities with timezones and then to use the Haversine formula to find the nearest city in that table, relevant to your coordinates. I have posted a full description here: http://sylnsr.blogspot.com/2012/12/find-nearest-location-by-latitude-and.html

For an example of loading the data in MySQL, I have posted an example here (with sources for downloading a small data dump): http://sylnsr.blogspot.com/2012/12/load-timezone-data-by-city-and-country.html

Note that the accuracy of the look-up will be based on how comprehensive your look-up data is.

Credits and References: MySQL Great Circle Distance (Haversine formula)

查看更多
家丑人穷心不美
6楼-- · 2019-01-03 23:51

You should be able to, if you know the polygon of the timezone to see if a given lat/lon is inside it.

World Time Zone Database enter image description here

Latitude/Longitude Polygon Data enter image description here

查看更多
劫难
7楼-- · 2019-01-03 23:58

For areas on land, there are some shapefile maps that have been made for the timezones of the tz (Olson) database. They're not updated quite as regularly as the tz database itself, but it's a great starting point and seems to be very accurate for most purposes.

查看更多
登录 后发表回答