From b4154c11d599fa3d7f14eee40dfd45047cc5046f Mon Sep 17 00:00:00 2001 From: Saeed Ranjbar Date: Tue, 19 Dec 2023 13:51:06 -0500 Subject: [PATCH] first commit --- latlong2.py | 42 ++++++++++++++++++++++++++++++++++++++++++ main.py | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 latlong2.py create mode 100644 main.py diff --git a/latlong2.py b/latlong2.py new file mode 100644 index 00000000..525b7b0a --- /dev/null +++ b/latlong2.py @@ -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]}") \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 00000000..9ca691dd --- /dev/null +++ b/main.py @@ -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))