-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils2.py
More file actions
31 lines (26 loc) · 1.08 KB
/
utils2.py
File metadata and controls
31 lines (26 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import string
from unidecode import unidecode
from shapely.geometry import shape
def str_wo_space(x):
return unidecode(x.translate(str.maketrans('', '', string.punctuation)).replace(' ', '-'))
from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="geoapiExercises")
def localisation(geometry):
centroid = shape(geometry).centroid
Latitude = str(centroid.y)
Longitude = str(centroid.x)
location = geolocator.reverse(Latitude+","+Longitude)
dict_loc = location.raw['address']
keys = [x for x in ['country_code', 'state', 'county', 'city'] if x in dict_loc.keys()]
name = '-'.join([ str_wo_space(dict_loc[key]) for key in keys])
return name, Latitude, Longitude
from OSMPythonTools.nominatim import Nominatim
from shapely import wkt
from rasterio.warp import transform_geom
from shapely.geometry import shape
def get_polygon(name, epsg):
finder = Nominatim()
spatial_entity = finder.query(name, wkt=True)
pol = wkt.loads(spatial_entity.wkt())
pol_ = shape(transform_geom('EPSG:4326', epsg, pol))
return pol_, spatial_entity