-
Notifications
You must be signed in to change notification settings - Fork 4
/
putSnowExPitsInSQL.py
74 lines (57 loc) · 2.28 KB
/
putSnowExPitsInSQL.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 14 20:45:44 2017
@author: hpm
"""
import pandas as pd
import sqlalchemy
import json
with open("postgres.json") as f:
db_conn_dict = json.load(f)
#Create secrete string!
cred_string = 'postgresql://{user}:{password}@{host}:{port}/{database}'.format(**db_conn_dict)
# create our SQL engine
dbeng = sqlalchemy.create_engine(cred_string)
# BELOW CODE WROTE CSV FILE TO A TABLE IN THE SQL DATABASE
#url="https://raw.githubusercontent.com/hpmarshall/snowex/master/Pit_Output/snowex_pit_out.csv"
#csv_file = pd.read_csv(url)
#csv_file.to_sql('SnowExPits', dbeng)
# BELOW CODE DOESN"T WORK>>>SOMETHING WRONG WITH ALTER statement
# now lets change these utm coordinates to lat/lon, or find the WGS UTM code for Colorado
#dbnamePts = 'SnowExPits'
#dbeng.execute("""ALTER TABLE %s ADD COLUMN geom geometry(Point, 4326);""" %(dbnamePts))
## populate the geometry field
#dbeng.execute("""UPDATE %s SET geom = ST_Transform(ST_setSRID(ST_MakePoint(x_utm13n,y_utm13n),32613),4326);""" %(dbnamePts))
# LETS READ IN SnowExPits table and plot
data=pd.read_sql_table('SnowExPits',dbeng)
# figure out how to make this DataFrame into a GeoDataFrame
import geopandas as gpd
dataXY=pd.DataFrame(data,columns=['x_utm13n','y_utm13n']) # grab just X/Y from data frame
from shapely.geometry import Point
geometry = [Point(xy) for xy in zip(dataXY['x_utm13n'], dataXY['y_utm13n'])]
dataGeo=gpd.GeoDataFrame(dataXY,geometry=geometry) # convert to geopandas
dataGeo.crs = {'init': 'epsg:32613'}
# ok now try transforming into lat/lon:
dataGeoLatLon=dataGeo.to_crs(epsg=4326)
# plot first for Grand Mesa study area
#fig, ax1 = plt.subplots()
# lets read in the basin boundary shape file
fig, ax1 = plt.subplots()
import matplotlib.pyplot as plt
SBBboundary=gpd.read_file('SenatorBeckBasinBoundary/poly.shp')
SBBboundaryLatLon=SBBboundary.to_crs(epsg=4326)
SBBboundaryLatLon.plot(ax=ax1)
#ax1=dataGeoLatLon.plot(color='red')
ax1.set_ylim(37.885,37.925)
ax1.set_xlim(-107.74,-107.705)
# Equivalent
#ax1.plot(dataGeoLatLon.lon, dataGeoLatLon.lat, 'ko')
dataGeoLatLon.plot(ax=ax1, color='k')
#ax1.set_ylim(38.97,39.15)
#ax1.set_xlim(-108.25,-107.85)
# Now for Senator Beck Study Site
#ax1=dataGeoLatLon.plot()
ax1.set_ylim(37.885,37.925)
ax1.set_xlim(-107.74,-107.705)
plt.show()