You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
2.2 KiB
65 lines
2.2 KiB
import os
|
|
import django
|
|
import argparse
|
|
from django.db import connection
|
|
|
|
# Setup Django environment
|
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.develop')
|
|
django.setup()
|
|
|
|
def search_cities(target_lat=None, target_lon=None):
|
|
print(f"\n{'='*80}")
|
|
print(f"SEARCHING CITIES BY INTEGER COORDINATE PART")
|
|
if target_lat is not None: print(f"Target Latitude Integer: {int(target_lat)}")
|
|
if target_lon is not None: print(f"Target Longitude Integer: {int(target_lon)}")
|
|
print(f"{'='*80}")
|
|
|
|
where_clauses = []
|
|
params = []
|
|
|
|
if target_lat is not None:
|
|
# TRUNC removes decimals. TRUNC(25.123) = 25. TRUNC(-25.123) = -25.
|
|
where_clauses.append("TRUNC(latitude) = %s")
|
|
params.append(int(target_lat))
|
|
|
|
if target_lon is not None:
|
|
where_clauses.append("TRUNC(longitude) = %s")
|
|
params.append(int(target_lon))
|
|
|
|
if not where_clauses:
|
|
print("Please provide at least --lat or --lon")
|
|
return
|
|
|
|
query = f"""
|
|
SELECT name, country_code, latitude, longitude, population
|
|
FROM geonames_city
|
|
WHERE {" AND ".join(where_clauses)}
|
|
ORDER BY population DESC NULLS LAST
|
|
LIMIT 50;
|
|
"""
|
|
|
|
with connection.cursor() as cursor:
|
|
cursor.execute(query, params)
|
|
results = cursor.fetchall()
|
|
|
|
if not results:
|
|
print("\n❌ No records found with those integer coordinates.")
|
|
return
|
|
|
|
print(f"\n{'Name':<30} | {'CC':<2} | {'Latitude':<12} | {'Longitude':<12} | {'Population':<10}")
|
|
print("-" * 80)
|
|
|
|
for name, cc, lat, lon, pop in results:
|
|
pop_str = f"{pop:,}" if pop else "N/A"
|
|
print(f"{name:<30} | {cc:<2} | {lat:<12.6f} | {lon:<12.6f} | {pop_str:<10}")
|
|
|
|
print(f"\nFound {len(results)} records (limited to top 50 by population).")
|
|
print(f"{'='*80}\n")
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser(description='Search cities by integer part of coordinates.')
|
|
parser.add_argument('--lat', type=float, help='Integer part of latitude to search for')
|
|
parser.add_argument('--lon', type=float, help='Integer part of longitude to search for')
|
|
|
|
args = parser.parse_args()
|
|
search_cities(args.lat, args.lon)
|