Commit 8081340a authored by Dorian's avatar Dorian
Browse files

clean the script than generate a geojson file

parent bbe7d340
import json
import sqlite3
from sqlite3 import Error
from pyproj import CRS
from pyproj import Transformer
import geojson
db_file = 'potelets.db'
geojson_file = 'potelets.geojson'
def create_connection(db_file):
""" create a database connection to a database that resides
in the memory
"""
conn = None;
try:
conn = sqlite3.connect(db_file)
conn.row_factory = sqlite3.Row
return conn
except Error as e:
print(e)
def get_potelets(conn):
""" return all the potelet cases from the sqlite db
"""
# sql = ''' SELECT * FROM potelets ORDER BY id DESC LIMIT 50'''
sql = ''' SELECT * FROM potelets ORDER BY id'''
cur = conn.cursor()
cur.execute(sql)
# convert from a list of sqlite3.Row objects to real dict objects
potelets = [dict(potelet) for potelet in cur.fetchall()]
for potelet in potelets:
# loads the dumped json
potelet["coordinates"] = json.loads(potelet["coordinates"])
return potelets
def convert_coord(coordinates, transformer):
""" convert the coordinates from EPSG:31370 to EPSG:4326
based on a transformer pyproj object
"""
x = coordinates["x"]
y = coordinates["y"]
new_x, new_y = transformer.transform(x, y)
return new_x, new_y
def gen_geojson(potelets, transformer):
""" generate valid geojson from the potelets list and put it in geojson_file
while converting the coord to GPS standard EPSG:4326
"""
features = []
for potelet in potelets:
x,y = convert_coord(potelet["coordinates"], transformer)
point = geojson.Point((x, y))
features.append(geojson.Feature(geometry=point, properties={"id": potelet["id"]}))
feature_collection = geojson.FeatureCollection(features)
with open(geojson_file, 'w') as f:
geojson.dump(feature_collection, f)
# --- Main ---
if __name__ == '__main__':
print('___ creating GEOJSON file from the potelets coordinates ___')
print('1. Establishing db connection')
conn = create_connection(db_file)
print('2. Making the sqlite request')
potelets = get_potelets(conn)
print('3. Initialisating geo coordinates systems convertion (epsg:31370 to epsg:4326)')
# EPSG:31370 that would be:
# proj4.defs("EPSG:31370","+proj=lcc +lat_1=51.16666723333333
# +lat_2=49.8333339 +lat_0=90 +lon_0=4.367486666666666 +x_0=150000.013
# +y_0=5400088.438 +ellps=intl
# +towgs84=-106.869,52.2978,-103.724,0.3366,-0.457,1.8422,-1.2747 +units=m
# +no_defs");
# For EPSG:4326 that would be:
# proj4.defs("EPSG:4326","+proj=longlat +datum=WGS84 +no_defs");
# define EPSG:31370 used by fixmystreet
crs_31370 = CRS.from_epsg(31370)
# define EPSG:4326 (also called WGS84), global GPS
crs_4326 = CRS.from_epsg(4326)
# create transformer
transformer = Transformer.from_crs(crs_31370, crs_4326, always_xy=True)
print('4. Generate geojson in ' + geojson_file)
gen_geojson(potelets, transformer)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment