Final versions working for Lake and Sumter
This commit is contained in:
+28
-2
@@ -14,17 +14,43 @@ TODO:
|
||||
import argparse
|
||||
import json
|
||||
import sys
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def get_county_area_id(county_name, state_name):
|
||||
"""Get OSM area ID for a county using Nominatim."""
|
||||
search_query = f"{county_name}, {state_name}, USA"
|
||||
url = f"https://nominatim.openstreetmap.org/search?q={urllib.parse.quote(search_query)}&format=json&limit=1&featuretype=county"
|
||||
|
||||
# Nominatim requires User-Agent header
|
||||
req = urllib.request.Request(url, headers={'User-Agent': 'TheVillagesImport/1.0'})
|
||||
|
||||
try:
|
||||
with urllib.request.urlopen(req) as response:
|
||||
results = json.loads(response.read().decode("utf-8"))
|
||||
|
||||
if results and results[0].get('osm_type') == 'relation':
|
||||
relation_id = int(results[0]['osm_id'])
|
||||
area_id = relation_id + 3600000000
|
||||
print(f"Found {county_name}, {state_name}: relation {relation_id} -> area {area_id}")
|
||||
return area_id
|
||||
|
||||
raise ValueError(f"Could not find relation for {county_name}, {state_name}")
|
||||
except urllib.error.HTTPError as e:
|
||||
print(f"Nominatim HTTP Error {e.code}: {e.reason}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def build_overpass_query(county_name, state_name, data_type="highways"):
|
||||
"""Build Overpass API query for specified data type in a county."""
|
||||
area_id = get_county_area_id(county_name, state_name)
|
||||
|
||||
base_query = f"""[out:json][timeout:60];
|
||||
area["name"="{state_name}"]["admin_level"="4"]->.state;
|
||||
area["name"="{county_name}"](area.state)->.searchArea;"""
|
||||
area(id:{area_id})->.searchArea;"""
|
||||
|
||||
if data_type == "highways":
|
||||
selector = '('
|
||||
|
||||
Reference in New Issue
Block a user