-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpy2gis.py
46 lines (35 loc) · 1.41 KB
/
py2gis.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""
@author: thomasmernagh
"""
#Packages
import pandas as pd
import geopandas as gpd
from sklearn.ensemble import IsolationForest
from shapely.geometry import Point, LineString, shape
#Import raw datafile
df41 = pd.read_csv("Week41.csv")
#Check and isolate outliers, exclude outliers
df41["outlier"] = IsolationForest().fit_predict(df41[["lat", "lon"]])
cleaned_df41 = df41[df41["outlier"] != -1]
cleaned_df41.to_csv("points_without_outliers41.csv")
#Rename X,Y for lat,lon
df41 = pd.read_csv("points_without_outliers41.csv")
print (df41)
df41.head()
df41['X'] = df41['lat']
df41['Y'] = df41['lon']
#combine lat,long to be geometries
# combine lat and lon column to a shapely Point() object
df41['geometry'] = df41.apply(lambda x: Point((float(x.lon), float(x.lat))), axis=1)
#Create points shapefiles
df41 = gpd.GeoDataFrame(df41, geometry='geometry')
df41.to_file('UtrechtW41.shp', driver='ESRI Shapefile')
#Import and read outliers file
df41 = pd.read_csv("points_without_outliers41.csv", sep='\s*,\s*')
#zip the coordinates into a point object and convert to a GeoData Frame
geometry41 = [Point(xy) for xy in zip(df41.lon, df41.lat)]
geo_df41 = gpd.GeoDataFrame(df41, geometry=geometry41)
geo_df41 = geo_df41.groupby(['sensor'])['geometry'].apply(lambda x:LineString(x.tolist()))
geo_df41 = gpd.GeoDataFrame(geo_df41, geometry='geometry')
#Output lines shape file
geo_df41.to_file('UtrechtW41_lines.shp', driver='ESRI Shapefile')