first commit
This commit is contained in:
parent
b66b0aa578
commit
b4154c11d5
42
latlong2.py
Normal file
42
latlong2.py
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
import requests
|
||||||
|
|
||||||
|
def get_lat_lng(api_key, address):
|
||||||
|
base_url = "https://api.opencagedata.com/geocode/v1/json"
|
||||||
|
params = {
|
||||||
|
'q': address,
|
||||||
|
'key': api_key,
|
||||||
|
}
|
||||||
|
|
||||||
|
response = requests.get(base_url, params=params)
|
||||||
|
data = response.json()
|
||||||
|
|
||||||
|
if response.status_code == 200 and data['status']['code'] == 200:
|
||||||
|
location = data['results'][0]['geometry']
|
||||||
|
return location['lat'], location['lng']
|
||||||
|
else:
|
||||||
|
print(f"Error: {data['status']['message']}")
|
||||||
|
return None, None
|
||||||
|
|
||||||
|
def get_lat_lng_for_addresses(api_key, addresses):
|
||||||
|
coordinates = []
|
||||||
|
|
||||||
|
for address in addresses:
|
||||||
|
lat, lng = get_lat_lng(api_key, address)
|
||||||
|
if lat is not None and lng is not None:
|
||||||
|
coordinates.append((lat, lng))
|
||||||
|
|
||||||
|
return coordinates
|
||||||
|
|
||||||
|
# Replace 'YOUR_API_KEY' with your OpenCage Geocoding API key
|
||||||
|
api_key = '744ad0d2d58e49d1ac57d6fb04fe5d82'
|
||||||
|
|
||||||
|
# Example list of addresses
|
||||||
|
addresses = ["1600 Amphitheatre Parkway, Mountain View, CA",
|
||||||
|
"Eiffel Tower, Paris, France",
|
||||||
|
"Big Ben, London, UK"]
|
||||||
|
|
||||||
|
coordinates = get_lat_lng_for_addresses(api_key, addresses)
|
||||||
|
|
||||||
|
# Print the coordinates
|
||||||
|
for address, coord in zip(addresses, coordinates):
|
||||||
|
print(f"{address} -> Latitude: {coord[0]}, Longitude: {coord[1]}")
|
40
main.py
Normal file
40
main.py
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
import json
|
||||||
|
from shapely import Polygon
|
||||||
|
from shapely import Point
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
# Make sure to enter your points in the clockwise direction
|
||||||
|
# and in the longitude, latitude format
|
||||||
|
#selection_box = Polygon([[-73.543833, 45.575932] ,
|
||||||
|
# [-73.541834, 45.575245],
|
||||||
|
# [-73.542275, 45.574652],
|
||||||
|
# [-73.544235, 45.575329],
|
||||||
|
# [-73.543833, 45.575932]])
|
||||||
|
x=-73.61574532884296
|
||||||
|
y=45.603911965131
|
||||||
|
diff=0.001
|
||||||
|
selection_box = Polygon([[x+diff, y-diff],
|
||||||
|
[x-diff, y-diff],
|
||||||
|
[x-diff, y+diff],
|
||||||
|
[x+diff, y+diff]])
|
||||||
|
geojson_file = Path('./data/collinear_clean 1.geojson').resolve()
|
||||||
|
output_file = Path('./output_buildings.geojson').resolve()
|
||||||
|
buildings_in_region = []
|
||||||
|
|
||||||
|
with open(geojson_file, 'r') as file:
|
||||||
|
city = json.load(file)
|
||||||
|
buildings = city['features']
|
||||||
|
|
||||||
|
for building in buildings:
|
||||||
|
coordinates = building['geometry']['coordinates'][0]
|
||||||
|
building_polygon = Polygon(coordinates)
|
||||||
|
centroid = Point(building_polygon.centroid)
|
||||||
|
|
||||||
|
if centroid.within(selection_box):
|
||||||
|
buildings_in_region.append(building)
|
||||||
|
|
||||||
|
output_region = {"type": "FeatureCollection",
|
||||||
|
"features": buildings_in_region}
|
||||||
|
|
||||||
|
with open(output_file, 'w') as file:
|
||||||
|
file.write(json.dumps(output_region, indent=2))
|
Loading…
Reference in New Issue
Block a user