Manage and analyse spatial data using shapely, geopandas and other libraries

 # Friday 02 - 10 - 2020 Malika Dakar, Senegal
# Self-study: AutoGis Helsinki, PRACTICE
#  Manage and analyse spatial data using shapely, geopandas and other libraries
""" Learning Goal
- Do Geocoding, transforme addresses into coordinates
- Construct point in Polygon queries
- Read DataFrame WFS and KML
- Make spatial join
- Construct nearest neighbor analysis
        
       You can download the ressources from  github.com/AutoGIS-2019
"""
##  1. GEOCODING
""" Geocoding is the process of transforming place, names or locations into coordinates
We will learn how to geocode addresses using geopandas and gopy.
There are several different geocoding services, they are available via geopy.
Geocoding services might require an API key in order to use them.
We will use NOMINATIM geocoder for relatively locating small number of addresses.
Nominatim based on OpenStreetMaps doesn't require an API key to use their services. 
"""
##  2.  Geocoding in GeoDataFrame
""" We will geocode addresses stored in a text file 'addresses.txt' """
import pandas as pd
import matplotlib.pyplot as plt
import geopandas as gpd
source_file = r'D:\Programm files\python\Scripts\Automatic Gis precess\lesson3\input files\addresses.txt'
data = pd.read_csv(source_file, sep=';')
# A very small note: Even a .txt file, you can read it in pandas in your way depends on your file style
"""
>>> data.head()
     id                                           addr
0  1000       Itämerenkatu 14, 00101 Helsinki, Finland
1  1001          Kampinkuja 1, 00100 Helsinki, Finland
2  1002           Kaivokatu 8, 00101 Helsinki, Finland
3  1003  Hermannin rantatie 1, 00580 Helsinki, Finland
4  1005     Tyynenmerenkatu 9, 00220 Helsinki, Finland
>>> data.columns.values
array(['id', 'addr'], dtype=object)
"""
## 3. Geocode addresses Using Nominatim
from geopandas.tools import geocode
geo = geocode(data['addr'], provider='nominatim', user_agent = 'autogis', timeout = 4)
# You will have the following result: (make sure you have internet to get the coordinates)
"""
>>> geo.head()
                    geometry                                            address
0  POINT (24.91556 60.16320)  Ruoholahti, 14, Itämerenkatu, Ruoholahti, Läns...
1  POINT (24.93169 60.16902)  Kamppi, 1, Kampinkuja, Kamppi, Eteläinen suurp...
2  POINT (24.94168 60.17001)  Na'am Kitchen, 8, Kaivokatu, Keskusta, Kluuvi,...
3  POINT (24.97783 60.18892)  Hermannin rantatie, Kalasatama, Sörnäinen, Hel...
4  POINT (24.92160 60.15665)  Hesburger, 9, Tyynenmerenkatu, Jätkäsaari, Län...
>>> geo.columns.values
array(['geometry', 'address'], dtype=object)
>>> geo.plot()
<AxesSubplot:>
>>> plt.show()
"""
#  4. Joining the two table
""" The first table was 'data' and the second is 'geo'. Joining them both together
"""
join = geo.join(data)
"""
>>> join.head()
                    geometry  ...                                           addr
0  POINT (24.91556 60.16320)  ...       Itämerenkatu 14, 00101 Helsinki, Finland
1  POINT (24.93169 60.16902)  ...          Kampinkuja 1, 00100 Helsinki, Finland
2  POINT (24.94188 60.17009)  ...           Kaivokatu 8, 00101 Helsinki, Finland
3  POINT (24.97783 60.18892)  ...  Hermannin rantatie 1, 00580 Helsinki, Finland
4  POINT (24.92160 60.15665)  ...     Tyynenmerenkatu 9, 00220 Helsinki, Finland
[5 rows x 4 columns]
>>> join.columns.values
array(['geometry', 'address', 'id', 'addr'], dtype=object)
>>> join.info()
<class 'geopandas.geodataframe.GeoDataFrame'>
Int64Index: 34 entries, 0 to 33
Data columns (total 4 columns):
 #   Column    Non-Null Count  Dtype   
---  ------    --------------  -----   
 0   geometry  34 non-null     geometry
 1   address   34 non-null     object  
 2   id        34 non-null     int64   
 3   addr      34 non-null     object  
dtypes: geometry(1), int64(1), object(2)
memory usage: 1.9+ KB
>>> join.describe()
                id
count    34.000000
mean   1017.382353
std      10.144456
min    1000.000000
25%    1009.250000
50%    1017.500000
75%    1025.750000
max    1034.000000
>>> join.plot()
<AxesSubplot:>
>>> plt.show()
"""
#----------------------------------- End ---------------------------------------------------##

Comments

Popular posts from this blog

Farming land size owned by households per region in Senegal