cleanup, proper handling of ignore_ids

This commit is contained in:
fordprefect
2020-03-26 19:06:12 +01:00
parent 50085391d1
commit 2d293f3a5c

View File

@@ -20,6 +20,7 @@ class OSMSearch():
self.maxsearchnums = args.pop("maxsearchnums") self.maxsearchnums = args.pop("maxsearchnums")
self.gpxfilename = args.pop("outputfilename") self.gpxfilename = args.pop("outputfilename")
self.use_boundingbox = args.pop("use_boundingbox") self.use_boundingbox = args.pop("use_boundingbox")
self.ignore_ids = args.pop("ignore_ids")
if self.use_boundingbox: if self.use_boundingbox:
assert isinstance(list, args["boundingbox"]) assert isinstance(list, args["boundingbox"])
assert len(args["boundingbox"]) == 4 assert len(args["boundingbox"]) == 4
@@ -36,11 +37,13 @@ class OSMSearch():
def initialize_gpx_file(self): def initialize_gpx_file(self):
if os.path.isfile(self.gpxfilename): if os.path.isfile(self.gpxfilename):
# open file and parse raise NotImplementedError("We don't know how to deal with existing files yet, sry")
#if verbosity > 0: print(f"found file, extending") # TODO: file name collision? either:
self.gpxfile = gpxpy.parse(open(gpxfilename, "r")) # 1) amend file
#self.gpxfile = gpxpy.parse(open(gpxfilename, "r"))
# 2) refuse to work
# 3) alter file name
else: else:
#if verbosity > 0: print("creating new file")
self.gpxfile = gpxpy.gpx.GPX() self.gpxfile = gpxpy.gpx.GPX()
def get_argstring(self): def get_argstring(self):
@@ -48,6 +51,8 @@ class OSMSearch():
if self.use_boundingbox: if self.use_boundingbox:
argstring.append(",".join(list(map(str, self.searchargs["boundingbox"])))) argstring.append(",".join(list(map(str, self.searchargs["boundingbox"]))))
if self.ignore_ids != []:
argstrings.append(f"ignore_ids=" + ",".join(list(map(str, self.ignore_ids))))
for arg in self.searchargs: for arg in self.searchargs:
argstrings.append(f"{arg}={self.searchargs[arg]}") argstrings.append(f"{arg}={self.searchargs[arg]}")
@@ -63,24 +68,21 @@ class OSMSearch():
r = requests.get(self.url + self.get_argstring()) r = requests.get(self.url + self.get_argstring())
if r.status_code != 200: if r.status_code != 200:
#if verbosity > 0: print(f"Query gone wrong: HTTP returned {r.status_code}")
exit(1) exit(1)
# parse reply # parse reply
reply = r.json() reply = r.json()
assert isinstance(reply, list), "Unexpected type of reply: " + type(reply) assert isinstance(reply, list), "Unexpected type of reply: " + type(reply)
if len(reply) == 0: if len(reply) == 0:
if verbosity > 0: print(f"found {len(ignore_ids)} results in {i-1} iterations, finishing")
break break
for point in reply: for point in reply:
self.searchargs["ignore_ids"].append(point["place_id"]) self.ignore_ids.append(point["place_id"])
self.gpxfile.waypoints.append(gpxpy.gpx.GPXWaypoint(latitude=point["lat"], longitude=point["lon"], name=point["display_name"])) self.gpxfile.waypoints.append(gpxpy.gpx.GPXWaypoint(latitude=point["lat"], longitude=point["lon"], name=point["display_name"]))
def save_to_disk(self): def save_to_disk(self):
with open(self.gpxfilename, "w") as f: with open(self.gpxfilename, "w") as f:
f.write(self.gpxfile.to_xml()) f.write(self.gpxfile.to_xml())
#if verbosity > 0: print(f"GPX file written to {gpxfilename}")
default_args = { default_args = {
"q": "Camping", "q": "Camping",