Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions address.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import psycopg2
from geopy.geocoders import Nominatim
import time

conn = psycopg2.connect(
host="localhost",
port=45432,
database="demo",
user="postgres",
password="postgres"
)
cur = conn.cursor()

cur.execute("""
CREATE TABLE IF NOT EXISTS Address (
address_id SERIAL PRIMARY KEY,
address_text TEXT,
address_x FLOAT,
address_y FLOAT
);
""")
conn.commit()

cur.execute("SELECT * FROM get_filtered_coordinates() LIMIT 10;")
results = cur.fetchall()

geolocator = Nominatim(user_agent="my_app")

for row in results:
lon = row[2]
lat = row[3]

try:
location = geolocator.reverse(f"{lat}, {lon}")
address = location.address if location else "Address not found"
except:
address = "Geocoding failed"

cur.execute("""
INSERT INTO Address (address_text, address_x, address_y)
VALUES (%s, %s, %s)
""", (address, lon, lat))
conn.commit()

time.sleep(1)

cur.close()
conn.close()
20 changes: 20 additions & 0 deletions func.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
CREATE OR REPLACE FUNCTION get_filtered_coordinates()
RETURNS TABLE (
airport_code character(3),
airport_name jsonb,
longitude double precision,
latitude double precision
) AS $$
BEGIN
RETURN QUERY
SELECT
a.airport_code,
a.airport_name,
a.coordinates[0] AS longitude,
a.coordinates[1] AS latitude
FROM airports_data a
WHERE
a.coordinates[1] BETWEEN 35 AND 50
AND a.coordinates[0] BETWEEN 35 AND 50;
END;
$$ LANGUAGE plpgsql;