forked from cpfair/tapiriik
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
50 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from tapiriik.database import tzdb | ||
from bson.son import SON | ||
|
||
def TZLookup(lat, lng): | ||
pt = [lng, lat] | ||
res = tzdb.boundaries.find_one({"Boundary": {"$geoIntersects": {"$geometry": {"type":"Point", "coordinates": pt}}}}, {"TZID": True}) | ||
if not res: | ||
res = tzdb.boundaries.find_one({"Boundary": SON([("$near", {"$geometry": {"type": "Point", "coordinates": pt}}), ("$maxDistance", 200000)])}, {"TZID": True}) | ||
res = res["TZID"] if res else None | ||
if not res or res == "uninhabited": | ||
res = round(lng / 15) | ||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# This file isn't called in normal operation, just to update the TZ boundary DB. | ||
# Should be called with `tz_world.*` files from http://efele.net/maps/tz/world/ in the working directory. | ||
# Requires pyshp and shapely for py3k (from https://github.com/mwtoews/shapely/tree/py3) | ||
|
||
import shapefile | ||
from shapely.geometry import Polygon, mapping | ||
import pymongo | ||
from tapiriik.database import tzdb | ||
|
||
print("Dropping boundaries collection") | ||
tzdb.drop_collection("boundaries") | ||
|
||
print("Setting up index") | ||
tzdb.boundaries.ensure_index([("Boundary", pymongo.GEOSPHERE)]) | ||
|
||
print("Reading shapefile") | ||
records = [] | ||
sf = shapefile.Reader("tz_world.shp") | ||
shapeRecs = sf.shapeRecords() | ||
|
||
ct = 0 | ||
total = len(shapeRecs) | ||
for shape in shapeRecs: | ||
tzid = shape.record[0] | ||
print("%3d%% %s" % (round(ct * 100 / total), tzid)) | ||
ct += 1 | ||
polygon = Polygon(list(shape.shape.points)) | ||
if not polygon.is_valid: | ||
polygon = polygon.buffer(0) # Resolves issues with most self-intersecting geometry | ||
assert polygon.is_valid | ||
record = {"TZID": tzid, "Boundary": mapping(polygon)} | ||
tzdb.boundaries.insert(record) # Would be bulk insert, but that makes it a pain to debug geometry issues |