Commit 667bc525 authored by Dorian's avatar Dorian
Browse files

clean

parent fb8b5882
#-----------------
# show all possibile history type
SELECT type FROM history GROUP BY type;
# show all type of actors
SELECT actor_type, potelet_id FROM attachments GROUP BY actor_type;
SELECT actor_type, potelet_id FROM history GROUP BY actor_type;
# show all the text from COMMENTS
# or all the href of PICTURES
SELECT text
FROM attachments
WHERE type='COMMENT';
# sort the actors by number of cases
SELECT responsibleOrganisation, count(*) AS c
FROM potelets
GROUP BY responsibleOrganisation
ORDER BY c;
# sort the actors by 'answering the most'
SELECT organisation, count(*) AS c
FROM attachments
GROUP BY organisation
ORDER BY c;
#-----------------
# potelets re-opened case by looking at attachment
SELECT potelet_id FROM attachment WHERE type='REOPEN_COMMENT';
# potelets re-opened case by looking at history
SELECT potelet_id FROM history WHERE type='INCIDENT_ASKED_REOPENING';
SELECT potelet_id FROM history WHERE type='INCIDENT_REOPENED';
# potelets marked as duplicates or severalOccurrence
SELECT * FROM potelets WHERE duplicates>0;
SELECT * FROM potelets WHERE severalOccurrence>0;
# => pas d'accès a ceux aux autres occurences ou duplicates...
# potelets where there is a lot of attachments
SELECT potelet_id, count(*) AS c
FROM attachments
GROUP BY potelet_id
HAVING c > 5;
# potelets where there is a lot of pictures/comment
SELECT potelet_id, count(*) AS c
FROM attachments
WHERE type='COMMENT'
GROUP BY potelet_id
HAVING c > 3;
SELECT potelet_id, count(*) AS c
FROM attachments
WHERE type='PICTURE'
GROUP BY potelet_id
HAVING c > 6;
# => got interesting result with this method (the one with a lot of comments)! look at 254080
# potelets where there is at least a professional actor responding
SELECT potelet_id, count(*) AS c
FROM attachments
WHERE type='COMMENT' and actor_type='PROFESSIONAL'
GROUP BY potelet_id
HAVING c > 0;
import json
import sqlite3
from sqlite3 import Error
# import geopy
# from geopy.geocoders import Nominatim
from pyproj import CRS
from pyproj import Transformer
import matplotlib.pyplot as plt
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 the last ??? potelet cases
"""
# sql = ''' SELECT * FROM potelets ORDER BY id DESC LIMIT 50'''
sql = ''' SELECT * FROM potelets ORDER BY id'''
# sql = ''' SELECT potelet_id FROM history WHERE type='INCIDENT_ASKED_REOPENING '''
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()]
# in the db most data are strings, we may want to loads them as python objects,
# or reformat the string, before inserting them in the jinja template.
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)
# print(str(new_x) + " " + str(new_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)
def plot(potelets, transformer):
""" plot the potelets list using matplotlib
"""
# fixed axes aspect ratio NOT WORKING
# fig, ax = plt.subplots()
# ax.set_aspect('equal')
plt.axes().set_aspect('equal')
# plt.gca().set_aspect('equal')
# subcats = ["missing", "damaged"]
# markers = ["o","o"]
# for i in range(len(subcats)):
# feed two list for ploting: one with all the x and another with all the y
x = []
y = []
colors = []
# marker = markers[i]
for potelet in potelets:
# if(potelet["subcat"] == subcats[i]):
potx,poty = convert_coord(potelet["coordinates"], transformer)
x.append(potx)
y.append(poty)
if(potelet["status"]=="CLOSED"):
colors.append('tab:green')
elif(potelet["status"]=="PROCESSING"):
colors.append('tab:orange')
elif(potelet["status"]=="CREATED"):
colors.append('tab:red')
else:
colors.append('tab:blue')
# ax.scatter(x, y, s=2, c=colors, marker=marker, label=subcats[i])
# ax.legend()
plt.scatter(x, y, s=2, c=colors)
plt.grid(True)
plt.show()
# --- Main ---
if __name__ == '__main__':
print('___ Ploting 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 (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)
# print('5. Initialisating plot')
# plot(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