drought/graphic/import/import_county_and_town.py

52 lines
1.9 KiB
Python
Raw Normal View History

2018-07-07 12:43:23 +00:00
import uuid
import psycopg2
G = 'host=210.77.68.250 port=5432 dbname=geobrought user=brought password=g214G214'
GT = 'host=210.77.68.250 port=5432 dbname=brought user=brought password=g214G214'
def insert_or_select_station(code, x, y):
with psycopg2.connect(GT) as connection:
with connection.cursor() as cursor:
cursor.execute('select id from graphic_countyboundary where code=%s', (code,))
result = cursor.fetchone()
if result:
return result[0]
else:
id = uuid.uuid4()
# geom = '{"type": "Point", "coordinates": [' + x + ', ' + y + ']}'
geom = '{"type": "Point", "coordinates": [' + str(x) + ',' + str(y) + ']}'
cursor.execute('insert into graphic_station (id, code, geom) values (%s, %s, %s )',
(str(id), code, geom))
connection.commit()
return id
def insert_grid(station_id, x, y):
id = uuid.uuid4()
geom = '{"type": "Point", "coordinates": [' + str(x) + ',' + str(y) + ']}'
with psycopg2.connect(GT) as connection:
with connection.cursor() as cursor:
cursor.execute('insert into graphic_grid(id, station_id, geom) values (%s,%s,%s)',
(str(id), str(station_id), geom))
connection.commit()
if __name__ == '__main__':
with psycopg2.connect(G) as connection:
with connection.cursor() as cursor:
cursor.execute('select * from bjtown')
results = cursor.fetchall()
for r in results:
print(r)
# grid_x = r[4]
# grid_y = r[5]
# station_code = r[8]
# station_x = r[9]
# station_y = r[10]
# station_id = insert_or_select_station(station_code, station_x, station_y)
# insert_grid(station_id, grid_x, grid_y)