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,59 +80,24 @@ 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"): alllats = []
import places alllongs = []
newplaces = [] for place in theplaces:
for place in theplaces: if place["lat"] != 0:
for location in places.places: alllats.append(place["lat"])
if place["name"] == location["name"]: if place["long"] != 0:
break alllongs.append(place["long"])
else: avglat = (max(alllats) + min(alllats))/2
newplaces.append(place["name"]) avglong = (max(alllongs) + min(alllongs))/2
if len(newplaces) > 0: writefile = open("placebuild/index.html","w")
with open("places.py","rb+") as editfile: writefile.write("<!DOCTYPE html>\n<html lang=\"en\">\n <head>\n <base target=\"_top\">\n <meta charset=\"utf-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"> \n <title>Map</title>\n <link rel=\"stylesheet\" href=\"")
editfile.seek(-2, os.SEEK_END) if local:
editfile.truncate() writefile.write(variables.domain)
appendfile = open("places.py","a") 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 newplaces: for place in theplaces:
appendfile.write(",\n{\"name\":\"" + place + "\",\"lat\":0,\"long\":0}") saniname = re.sub(" ","",(re.sub("","",(re.sub("-","",place["name"])))))
appendfile.write("]") writefile.write(" const " + saniname + " = L.marker([" + str(place["lat"]) + ", " + str(place["long"]) + "]).addTo(map).bindTooltip('<h2>" + place["name"] + "</h2><ul>")
appendfile.close() for date in place["dates"]:
print("Add latitudes and longitudes in places.py before running again") writefile.write("<li><code>" + date + "</code></li>")
else: writefile.write("</ul>');\n")
alllats = [] writefile.write(" </script>\n </body>\n</html>")
alllongs = []
import places
for place in places.places:
if place["lat"] != 0:
alllats.append(place["lat"])
if place["long"] != 0:
alllongs.append(place["long"])
avglat = (max(alllats) + min(alllats))/2
avglong = (max(alllongs) + min(alllongs))/2
writefile = open("placebuild/index.html","w")
writefile.write("<!DOCTYPE html>\n<html lang=\"en\">\n <head>\n <base target=\"_top\">\n <meta charset=\"utf-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"> \n <title>Map</title>\n <link rel=\"stylesheet\" href=\"")
if local:
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")
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"])))))
writefile.write(" const " + saniname + " = L.marker([" + str(thelat) + ", " + str(thelong) + "]).addTo(map).bindTooltip('<h2>" + place["name"] + "</h2><ul>")
for date in place["dates"]:
writefile.write("<li><code>" + date + "</code></li>")
writefile.write("</ul>');\n")
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