Get latitude and longitude automatically

master
trémeur 6 months ago
parent ff76322a82
commit 2886ca7192

1
.gitignore vendored

@ -5,5 +5,4 @@ gamebuild/*
!gamebuild/*.png !gamebuild/*.png
placebuild/* placebuild/*
!placebuild/.gitkeep !placebuild/.gitkeep
places.py
variables.py variables.py

@ -3,8 +3,9 @@
* About * About
The scripts in this repository take information from a structured set of =.org= files and use them to build static webpages that track various activities (so far, gaming and travelling). These pages can be used as a replacement for third-party services e.g. Backloggery. The scripts in this repository take information from a structured set of =.org= files and use them to build static webpages that track various activities (so far, gaming and travelling). These pages can be used as a replacement for third-party services e.g. Backloggery.
* Dependencies * Dependencies
- =python3= - =geopy= if you are tracking places visited (install via pip)
- =orgparse= (install via pip) - =orgparse= (install via pip)
- =python3=
- =rclone= (for syncing to the server) - =rclone= (for syncing to the server)
* General setup * General setup
** Structure of the .org files ** Structure of the .org files
@ -40,8 +41,8 @@ In =variables.py=, edit:
- =placestartyear=: year from which to begin tracking (an integer) - =placestartyear=: year from which to begin tracking (an integer)
- =placecss=: location of the CSS file you wish to apply, relative to the site root - =placecss=: location of the CSS file you wish to apply, relative to the site root
** Testing ** Testing
- Run =python3 generateplaces.py local= to build in =placebuild=. - Run =python3 generateplaces.py local= to build in =placebuild=. (This may take some time.)
- If new places have been added since the last build, they will be added to =places.py= and the terminal will signal this. Add the latitude and longitude for these places in =places.py= before building again. - Check whether places are displayed accurately in the output file. If not, add regions/countries to disambiguate any incorrectly displayed places to the =disambig= dictionary in =variables.py= (see examples).
* First run (or if adding any new trackers) * First run (or if adding any new trackers)
- Run =python3 init.py= and then =chmod +x build.sh=. - Run =python3 init.py= and then =chmod +x build.sh=.
- Create directories on the server corresponding to any =*serverpath= variables you have set. - Create directories on the server corresponding to any =*serverpath= variables you have set.

@ -1,6 +1,6 @@
import orgparse,os,re,sys,variables import orgparse,os,re,sys,variables
from datetime import datetime from datetime import datetime
import sys,variables from geopy.geocoders import Nominatim
try: try:
if sys.argv[1] == "local": if sys.argv[1] == "local":
@ -10,7 +10,7 @@ try:
except: except:
local = False local = False
if variables.trackgames == True: if variables.trackplaces == True:
thisyear = datetime.now().strftime("%Y") thisyear = datetime.now().strftime("%Y")
@ -18,6 +18,8 @@ if variables.trackgames == True:
concernedfiles = [] concernedfiles = []
geo = Nominatim(user_agent="python3")
while year < int(thisyear) + 1: while year < int(thisyear) + 1:
month = 0 month = 0
while month < 13: while month < 13:
@ -59,7 +61,11 @@ if variables.trackgames == True:
else: else:
dates = filedate dates = filedate
if status == "new": if status == "new":
thedict = {"name":placename,"dates":[dates]} try:
lookup = placename + ", " + variables.disambig[placename]
except:
lookup = placename
thedict = {"name":placename,"dates":[dates],"lat":geo.geocode(lookup).latitude,"long":geo.geocode(lookup).longitude}
places.append(thedict) places.append(thedict)
else: else:
twodict = {"name":placename,"dates":[dates]} twodict = {"name":placename,"dates":[dates]}
@ -74,30 +80,9 @@ if variables.trackgames == True:
theplaces = sorted(places,key=lambda d: d["name"]) theplaces = sorted(places,key=lambda d: d["name"])
if os.path.isfile("places.py"):
import places
newplaces = []
for place in theplaces:
for location in places.places:
if place["name"] == location["name"]:
break
else:
newplaces.append(place["name"])
if len(newplaces) > 0:
with open("places.py","rb+") as editfile:
editfile.seek(-2, os.SEEK_END)
editfile.truncate()
appendfile = open("places.py","a")
for place in newplaces:
appendfile.write(",\n{\"name\":\"" + place + "\",\"lat\":0,\"long\":0}")
appendfile.write("]")
appendfile.close()
print("Add latitudes and longitudes in places.py before running again")
else:
alllats = [] alllats = []
alllongs = [] alllongs = []
import places for place in theplaces:
for place in places.places:
if place["lat"] != 0: if place["lat"] != 0:
alllats.append(place["lat"]) alllats.append(place["lat"])
if place["long"] != 0: if place["long"] != 0:
@ -110,23 +95,9 @@ if variables.trackgames == True:
writefile.write(variables.domain) writefile.write(variables.domain)
writefile.write(variables.placecss + "\"/>\n <link rel=\"stylesheet\" href=\"https://unpkg.com/leaflet@1.9.4/dist/leaflet.css\" integrity=\"sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY=\" crossorigin=\"\"/>\n <script src=\"https://unpkg.com/leaflet@1.9.4/dist/leaflet.js\" integrity=\"sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=\" crossorigin=\"\"></script>\n <style>\n #map {\n width: 90vw;\n height: 90vh;\n margin: auto;\n }\n </style>\n </head>\n <body>\n <div id=\"map\" style=\"width: 90vw; height: 90vh;\"></div>\n <script>\n const map = L.map('map').fitBounds([[" + str(min(alllats)) + ", " + str(min(alllongs)) + "],[" + str(max(alllats)) + ", " + str(max(alllongs)) + "]]);\n const tiles = L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {\n attribution: '&copy; <a href=\"http://www.openstreetmap.org/copyright\">OpenStreetMap</a>'\n }).addTo(map);\n") writefile.write(variables.placecss + "\"/>\n <link rel=\"stylesheet\" href=\"https://unpkg.com/leaflet@1.9.4/dist/leaflet.css\" integrity=\"sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY=\" crossorigin=\"\"/>\n <script src=\"https://unpkg.com/leaflet@1.9.4/dist/leaflet.js\" integrity=\"sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=\" crossorigin=\"\"></script>\n <style>\n #map {\n width: 90vw;\n height: 90vh;\n margin: auto;\n }\n </style>\n </head>\n <body>\n <div id=\"map\" style=\"width: 90vw; height: 90vh;\"></div>\n <script>\n const map = L.map('map').fitBounds([[" + str(min(alllats)) + ", " + str(min(alllongs)) + "],[" + str(max(alllats)) + ", " + str(max(alllongs)) + "]]);\n const tiles = L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {\n attribution: '&copy; <a href=\"http://www.openstreetmap.org/copyright\">OpenStreetMap</a>'\n }).addTo(map);\n")
for place in theplaces: for place in theplaces:
for location in places.places:
if location["name"] == place["name"]:
thelat = location["lat"]
thelong = location["long"]
saniname = re.sub(" ","",(re.sub("","",(re.sub("-","",place["name"]))))) saniname = re.sub(" ","",(re.sub("","",(re.sub("-","",place["name"])))))
writefile.write(" const " + saniname + " = L.marker([" + str(thelat) + ", " + str(thelong) + "]).addTo(map).bindTooltip('<h2>" + place["name"] + "</h2><ul>") writefile.write(" const " + saniname + " = L.marker([" + str(place["lat"]) + ", " + str(place["long"]) + "]).addTo(map).bindTooltip('<h2>" + place["name"] + "</h2><ul>")
for date in place["dates"]: for date in place["dates"]:
writefile.write("<li><code>" + date + "</code></li>") writefile.write("<li><code>" + date + "</code></li>")
writefile.write("</ul>');\n") writefile.write("</ul>');\n")
writefile.write(" </script>\n </body>\n</html>") writefile.write(" </script>\n </body>\n</html>")
else:
writefile = open("places.py","w")
writefile.write("places = [")
for place in theplaces:
writefile.write("{\"name\":\"" + place["name"] + "\",\"lat\":0,\"long\":0}")
if place != theplaces[-1]:
writefile.write(",\n")
writefile.write("]")
writefile.close()
print("Add latitudes and longitudes in places.py before running again")

@ -46,3 +46,5 @@ placeserverpath = ""
placestartyear = 0 placestartyear = 0
placecss = "" placecss = ""
# disambig = {"london":"england","belfast":"northern ireland"}

Loading…
Cancel
Save