Skip to content
Snippets Groups Projects
Commit 07754440 authored by Duchateau Fabien's avatar Duchateau Fabien
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
Pipeline #353 failed with stages
This diff is collapsed.
README 0 → 100644
# mongiris package
This package is an interface for querying INSEE IRIS stored as documents in MongoDB.
Requires loading the IRIS files into MongoDB prior to using this package.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#__version__ = "0.1"
from .mongiris import Mongiris
# all modules
__all__ = ['Mongiris']
{
"type": "FeatureCollection",
"features": [
{"geometry": {"type": "Polygon", "coordinates": [[[2.1259032, 50.9948769], [2.125942, 50.994853], [2.126146, 50.9947297], [2.1262322, 50.9946792], [2.1264301, 50.9945783], [2.126777, 50.9944139], [2.1279759, 50.9938877], [2.1289943, 50.9934355], [2.1295251, 50.9931997], [2.1298797, 50.9930353], [2.1300819, 50.9929345], [2.1302785, 50.9928264], [2.1304726, 50.9926986], [2.1305085, 50.9926746], [2.1308537, 50.992442], [2.1324174, 50.9913648], [2.133694, 50.9897968], [2.1336878, 50.9897411], [2.1337002, 50.9896856], [2.1337392, 50.9896491], [2.1337795, 50.9896189], [2.1337867, 50.9896144], [2.1357203, 50.9883424], [2.135824, 50.9882597], [2.1358865, 50.9881803], [2.1359831, 50.9880177], [2.1360449, 50.9878962], [2.1361951, 50.987594], [2.136272, 50.9874223], [2.1362886, 50.9873641], [2.1363068, 50.9873006], [2.1363302, 50.9871805], [2.1363391, 50.9870756], [2.1363325, 50.9869662], [2.1363229, 50.986862], [2.1362965, 50.9867479], [2.1362461, 50.986622], [2.1362215, 50.9865662], [2.1362023, 50.9865257], [2.1361154, 50.98637], [2.1349595, 50.9843581], [2.1348308, 50.9841536], [2.1347489, 50.9840445], [2.1345718, 50.9838513], [2.1343998, 50.9836868], [2.134257, 50.9835648], [2.1341072, 50.9834372], [2.1339264, 50.9832907], [2.1337931, 50.9831938], [2.1337467, 50.9831648], [2.1336398, 50.9831003], [2.1336272, 50.9830931], [2.1334794, 50.9830113], [2.1330101, 50.9827856], [2.1309622, 50.9818224], [2.1303349, 50.9815319], [2.1302855, 50.9815091], [2.1301078, 50.9814352], [2.1299595, 50.9813848], [2.1298137, 50.9813506], [2.1296692, 50.9813245], [2.1295103, 50.9813117], [2.1293782, 50.9813108], [2.1293071, 50.9813121], [2.1292175, 50.9813142], [2.1290439, 50.9813318], [2.128907, 50.9813587], [2.1287642, 50.9813989], [2.1286068, 50.9814579], [2.1284463, 50.9815358], [2.1281691, 50.9817047], [2.127892, 50.9818692], [2.1277001, 50.9819512], [2.1275058, 50.9820145], [2.1273301, 50.9820662], [2.1271519, 50.9820963], [2.1271291, 50.9820989], [2.1269783, 50.982114], [2.1267479, 50.9821222], [2.1265235, 50.9821162], [2.1262881, 50.9820867], [2.1260716, 50.9820359], [2.125902, 50.9819818], [2.1257694, 50.9819297], [2.1255717, 50.981818], [2.1254852, 50.9817671], [2.1254135, 50.9817245], [2.1242958, 50.9810385], [2.123704, 50.9806729], [2.122357, 50.9788934], [2.1218624, 50.9779358], [2.1222013, 50.9758925], [2.1227618, 50.9738984], [2.1232622, 50.9722675], [2.1242753, 50.9713256], [2.1254577, 50.9704583], [2.1254637, 50.970436], [2.1253546, 50.9704199], [2.1251648, 50.9703863], [2.1251457, 50.9705082], [2.125058, 50.9707273], [2.1249423, 50.9708458], [2.12471, 50.9709635], [2.1228779, 50.9725902], [2.1220575, 50.9741177], [2.1217316, 50.9760798], [2.1215569, 50.9779318], [2.1227102, 50.980221], [2.1196805, 50.9807761], [2.1168031, 50.9815124], [2.1143213, 50.9825161], [2.1115859, 50.9836269], [2.1097467, 50.9842852], [2.1095867, 50.9843307], [2.1093843, 50.984367], [2.1092306, 50.9843766], [2.1092331, 50.9843991], [2.1085032, 50.986039], [2.1084753, 50.9863231], [2.1096488, 50.9875389], [2.1098974, 50.9877012], [2.1100464, 50.9877175], [2.1100719, 50.9877186], [2.1103201, 50.9878307], [2.1152976, 50.9899479], [2.1194701, 50.9914995], [2.1199155, 50.991709], [2.1203856, 50.9919706], [2.1204431, 50.9920159], [2.1206842, 50.9918059], [2.1218681, 50.9907566], [2.1219779, 50.9906542], [2.1220142, 50.9906123], [2.1220348, 50.9905703], [2.1220442, 50.9905193], [2.1220467, 50.9904601], [2.1220314, 50.9903631], [2.1219958, 50.9902014], [2.1219071, 50.9899137], [2.1218542, 50.9897698], [2.1218203, 50.9896781], [2.1217721, 50.989589], [2.1209458, 50.9882529], [2.1204289, 50.9875066], [2.1197814, 50.9865478], [2.1191644, 50.9854785], [2.1201966, 50.9851252], [2.1203366, 50.9850849], [2.120572, 50.9850355], [2.1209256, 50.9852882], [2.1210531, 50.9853923], [2.1226011, 50.9868213], [2.123839, 50.9897245], [2.1234482, 50.9909345], [2.1227991, 50.9915171], [2.1221361, 50.9922439], [2.1214931, 50.9930808], [2.1223669, 50.9938285], [2.1238122, 50.9940819], [2.125345, 50.9945349], [2.1258611, 50.9948488], [2.1259032, 50.9948769]]]}, "type": "Feature", "properties": {"IRIS": "0103", "NOM_IRIS": "centre", "TYP_IRIS": "H", "DEP": "59", "INSEE_COM": "59273", "NOM_COM": "Gravelines", "REG": "32", "CODE_IRIS": "592730103"}}
]
}
Edges 58 and 66 cross. Edge locations in degrees: [2.1228779, 50.9725902]-[2.1247100, 50.9709635] and [2.1254577, 50.9704583]-[2.1242753, 50.9713256]"
db.iris.updateOne({"_id": ObjectId("5bdb6b96f3f0b9036b0c9580")},
{$set: {"geometry": {
"type": "Polygon",
"coordinates": [
[
[
2.1259032,
50.9948769
],
[
2.1258611,
50.9948488
],
[
2.125345,
50.9945349
],
[
2.1238122,
50.9940819
],
[
2.1223669,
50.9938285
],
[
2.1214931,
50.9930808
],
[
2.1221361,
50.9922439
],
[
2.1227991,
50.9915171
],
[
2.1234482,
50.9909345
],
[
2.123839,
50.9897245
],
[
2.1226011,
50.9868213
],
[
2.1210531,
50.9853923
],
[
2.1209256,
50.9852882
],
[
2.120572,
50.9850355
],
[
2.1203366,
50.9850849
],
[
2.1201966,
50.9851252
],
[
2.1191644,
50.9854785
],
[
2.1197814,
50.9865478
],
[
2.1204289,
50.9875066
],
[
2.1209458,
50.9882529
],
[
2.1217721,
50.989589
],
[
2.1218203,
50.9896781
],
[
2.1218542,
50.9897698
],
[
2.1219071,
50.9899137
],
[
2.1219958,
50.9902014
],
[
2.1220314,
50.9903631
],
[
2.1220467,
50.9904601
],
[
2.1220442,
50.9905193
],
[
2.1220348,
50.9905703
],
[
2.1220142,
50.9906123
],
[
2.1219779,
50.9906542
],
[
2.1218681,
50.9907566
],
[
2.1206842,
50.9918059
],
[
2.1204431,
50.9920159
],
[
2.1203856,
50.9919706
],
[
2.1199155,
50.991709
],
[
2.1194701,
50.9914995
],
[
2.1152976,
50.9899479
],
[
2.1103201,
50.9878307
],
[
2.1100719,
50.9877186
],
[
2.1100464,
50.9877175
],
[
2.1098974,
50.9877012
],
[
2.1096488,
50.9875389
],
[
2.1084753,
50.9863231
],
[
2.1085032,
50.986039
],
[
2.1092331,
50.9843991
],
[
2.1092306,
50.9843766
],
[
2.1093843,
50.984367
],
[
2.1095867,
50.9843307
],
[
2.1097467,
50.9842852
],
[
2.1115859,
50.9836269
],
[
2.1143213,
50.9825161
],
[
2.1168031,
50.9815124
],
[
2.1196805,
50.9807761
],
[
2.1227102,
50.980221
],
[
2.1215569,
50.9779318
],
[
2.1217316,
50.9760798
],
[
2.1220575,
50.9741177
],
[
2.1228779,
50.9725902
],
[
2.12471,
50.9709635
],
[
2.1249423,
50.9708458
],
[
2.125058,
50.9707273
],
[
2.1251457,
50.9705082
],
[
2.1251648,
50.9703863
],
[
2.1253546,
50.9704199
],
[
2.1254637,
50.970436
],
[
2.1254577,
50.9704583
],
[
2.1242753,
50.9713256
],
[
2.1232622,
50.9722675
],
[
2.1227618,
50.9738984
],
[
2.1222013,
50.9758925
],
[
2.1218624,
50.9779358
],
[
2.122357,
50.9788934
],
[
2.123704,
50.9806729
],
[
2.1242958,
50.9810385
],
[
2.1254135,
50.9817245
],
[
2.1254852,
50.9817671
],
[
2.1255717,
50.981818
],
[
2.1257694,
50.9819297
],
[
2.125902,
50.9819818
],
[
2.1260716,
50.9820359
],
[
2.1262881,
50.9820867
],
[
2.1265235,
50.9821162
],
[
2.1267479,
50.9821222
],
[
2.1269783,
50.982114
],
[
2.1271291,
50.9820989
],
[
2.1271519,
50.9820963
],
[
2.1273301,
50.9820662
],
[
2.1275058,
50.9820145
],
[
2.1277001,
50.9819512
],
[
2.127892,
50.9818692
],
[
2.1281691,
50.9817047
],
[
2.1284463,
50.9815358
],
[
2.1286068,
50.9814579
],
[
2.1287642,
50.9813989
],
[
2.128907,
50.9813587
],
[
2.1290439,
50.9813318
],
[
2.1292175,
50.9813142
],
[
2.1293071,
50.9813121
],
[
2.1293782,
50.9813108
],
[
2.1295103,
50.9813117
],
[
2.1296692,
50.9813245
],
[
2.1298137,
50.9813506
],
[
2.1299595,
50.9813848
],
[
2.1301078,
50.9814352
],
[
2.1302855,
50.9815091
],
[
2.1303349,
50.9815319
],
[
2.1309622,
50.9818224
],
[
2.1330101,
50.9827856
],
[
2.1334794,
50.9830113
],
[
2.1336272,
50.9830931
],
[
2.1336398,
50.9831003
],
[
2.1337467,
50.9831648
],
[
2.1337931,
50.9831938
],
[
2.1339264,
50.9832907
],
[
2.1341072,
50.9834372
],
[
2.134257,
50.9835648
],
[
2.1343998,
50.9836868
],
[
2.1345718,
50.9838513
],
[
2.1347489,
50.9840445
],
[
2.1348308,
50.9841536
],
[
2.1349595,
50.9843581
],
[
2.1361154,
50.98637
],
[
2.1362023,
50.9865257
],
[
2.1362215,
50.9865662
],
[
2.1362461,
50.986622
],
[
2.1362965,
50.9867479
],
[
2.1363229,
50.986862
],
[
2.1363325,
50.9869662
],
[
2.1363391,
50.9870756
],
[
2.1363302,
50.9871805
],
[
2.1363068,
50.9873006
],
[
2.1362886,
50.9873641
],
[
2.136272,
50.9874223
],
[
2.1361951,
50.987594
],
[
2.1360449,
50.9878962
],
[
2.1359831,
50.9880177
],
[
2.1358865,
50.9881803
],
[
2.135824,
50.9882597
],
[
2.1357203,
50.9883424
],
[
2.1337867,
50.9896144
],
[
2.1337795,
50.9896189
],
[
2.1337392,
50.9896491
],
[
2.1337002,
50.9896856
],
[
2.1336878,
50.9897411
],
[
2.133694,
50.9897968
],
[
2.1324174,
50.9913648
],
[
2.1308537,
50.992442
],
[
2.1305085,
50.9926746
],
[
2.1304726,
50.9926986
],
[
2.1302785,
50.9928264
],
[
2.1300819,
50.9929345
],
[
2.1298797,
50.9930353
],
[
2.1295251,
50.9931997
],
[
2.1289943,
50.9934355
],
[
2.1279759,
50.9938877
],
[
2.126777,
50.9944139
],
[
2.1264301,
50.9945783
],
[
2.1262322,
50.9946792
],
[
2.126146,
50.9947297
],
[
2.125942,
50.994853
],
[
2.1259032,
50.9948769
]
]
]
}
}
})
\ No newline at end of file
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# =============================================================================
# Abstraction layer for the MongoDB database
# Performs operations such as find, update, convert_geojson_files, intersect, etc.
# =============================================================================
# Export et import d'une collection MongoDB (plus rapide, inclut index et métadonnes mais binaire, option --gzip)
# ./mongodump --db db_hil --collection iris --archive=dump-iris
# ./mongorestore --archive=dump-iris
# Export et import du contenu d'une collection MongoDB (option --pretty pour affichage, mais volumineux) :
# ./mongoexport --db db_hil --collection iris --out ~/dump-iris.json
# ./mongoimport --db db_hil --collection iris --file ~/dump-iris.json
# =============================================================================
import pymongo
from bson import json_util # used to convert BSON to JSON (especially ObjectId type of "_id")
import json
import logging
class Mongiris:
def __init__(self):
logging.basicConfig(format='[%(levelname)s] - %(name)s - %(asctime)s : %(message)s')
self.logger = logging.getLogger()
self.logger.setLevel(logging.INFO)
self.connection = self.init_connection() # default MongoDB connection on 'localhost', 27017
self.database_hil = self.connection.db_hil # database for HiL project
self.iris_collection = self.database_hil.iris # iris collection
@staticmethod
def bson_to_json(doc_bson):
"""
Converts the bson data to valid JSON (including the ObjectId type converted to {"$oid": <string>}
:param doc_bson: the BSON data to be converted
:return: a JSON object
"""
doc_json = json.loads(json_util.dumps(doc_bson, json_options=json_util.RELAXED_JSON_OPTIONS))
return doc_json
def init_connection(self):
max_timeout = 3 # delay before connection timeout
connection = None
try:
connection = pymongo.MongoClient(serverSelectionTimeoutMS=max_timeout) # default on 'localhost', 27017
connection.server_info() # forces a query to MongoDB (for checking the connection)
except pymongo.errors.ServerSelectionTimeoutError as e:
self.logger.error('Could not connect to the MongoDB database ! Have you launched MongoDB ? ' + str(e))
return connection
def _parse_json_to_dict(self, json_file_path):
with open(json_file_path) as data_file:
data = json.load(data_file)
data_file.close()
return data
def _save_dict_to_json(self, json_file_path, dict_geo):
with open(json_file_path, 'w') as data_file:
json.dump(dict_geo, data_file)
def convert_geojson_files_to_mongo(self, ):
"""
This method should not be used (already run once). Implemented for inserting geojson files into MongoDB.
:return: the number of iris inserted in the collection
#TODO add the parsing of a dictionary file (open "01-dico.json", loop and store each of its dict as a Mongo doc)
"""
import os
from os import path
# print(os.getcwd()) # check working directory, and set it to Hil-quartiers in project settings
path_hil = path.join('..', 'HiL-recommender')
web_dir = path.join(path_hil, 'static', 'data') # from HiL-recommender.config
geojson_integrated_output_departement_dir = path.join(web_dir, 'iris_by_departments') # from HiL-recommender.config
self.logger.info("#documents in collection " + str(self.count_documents(self.iris_collection, {})))
self.iris_collection.delete_many({}) # empty collection
self.logger.info("#documents in collection " + str(self.count_documents(self.iris_collection, {})))
for file in os.listdir(geojson_integrated_output_departement_dir):
if file.endswith('.geojson'): # read each geojson (department) file and insert each iris in MongoDB
geojson = self._parse_json_to_dict(path.join(geojson_integrated_output_departement_dir, file))
try:
# all_iris = geojson['features'] # get a list of iris (geojson dict)
# result = iris_collection.insert_many(all_iris) # pb of 592730103 iris, and many 59 iris not inserted
for iris in geojson['features']: # inserting each iris one by one
if iris["properties"]["CODE_IRIS"] != "592730103": # pb of 592730103 iris in Gravelines
result = self.iris_collection.insert_one(iris) # result.inserted_id
logging.info("Documents inserted for file " + file)
except Exception as e:
self.logger.error('Error with MongoDB connection: ' + str(e))
#TODO delete next line (which removes one IRIS in Gravelines). Its GeoJSON is valid, but it has 2 edges (very close)
#TODO that cross according to MOngoDB (and GDAL), which avoid the creation of an index. See file iris_errno.geojson
#TODO CODE_IRIS = 592730103
self.iris_collection.delete_one({"properties.CODE_IRIS": 592730103}) # should not be found, but just in case
nb_docs = self.count_documents(self.iris_collection, {})
self.logger.info("#documents in collection " + str(nb_docs))
self.logger.info("Creating index on 'geometry' using " + pymongo.GEOSPHERE)
self.iris_collection.create_index([("geometry", pymongo.GEOSPHERE)])
self.logger.info("Index created")
assert (nb_docs == 49403), 'Error: expecting 49403 IRIS to be stored in MongoDB, but stored %i' % nb_docs
return nb_docs
def count_documents(self, collection, json_query):
"""
Counts the number of documents that satisfy json_query in the given collection
:param collection: the collection to count in
:param json_query: the query criteria
:return: the number of documents
"""
return collection.count_documents(json_query)
def find_one_document(self, collection, json_query):
"""
Finds the first document in the given collection that satisfies json_query
:param collection: the collection to search in
:param json_query: the query criteria
:return: a json document or None
"""
doc = collection.find_one(json_query)
doc_json = Mongiris.bson_to_json(doc)
return doc_json
def get_iris_from_code(self, code_iris):
iris = self.find_one_document(self.iris_collection, {"properties.CODE_IRIS": code_iris})
return iris
def find_documents(self, collection, json_query, json_projection=None):
"""
Finds the first document in the given collection that satisfies json_query
:param collection: the collection to search in
:param json_query: the query criteria
:param json_projection: a json document indicating the fields that appear in the results
:return: a cursor (set of documents)
"""
cursor = collection.find(json_query, json_projection)
doc_json = Mongiris.bson_to_json(cursor)
return doc_json
def get_random_document(self, collection):
# return a random document from the given collection
random_iris = collection.aggregate([{"$sample": {"size": 1}}]).next()
doc_json = Mongiris.bson_to_json(random_iris)
return doc_json
def geo_within(self, collection, geometry, json_projection=None):
"""
Find all documents from given collection and which contain totally the given geometry
Cannot be used to find the IRIS containing a point (geometry must be a polygon)
:param collection: the collection to search in
:param geometry: a geojson geometry ("Polygon" or "MultiPolygon", NO "Point")
:param json_projection: a json document indicating the fields that appear in the results
:return: a cursor (set of documents)
"""
cursor = self.find_documents(collection, {"geometry": {"$geoWithin": {
"$geometry": geometry}}}, json_projection)
doc_json = Mongiris.bson_to_json(cursor)
return doc_json
def geo_within_sphere(self, collection, sphere, json_projection=None):
"""
Find all documents from given collection and which contain totally the given sphere
Cannot be used to find the IRIS containing a point (geometry must be a polygon, with min. 3 points)
:param collection: the collection to search in
:param sphere: a geojson geometry defined by a center and a radius in radians
:param json_projection: a json document indicating the fields that appear in the results
:return: a cursor (set of documents)
"""
cursor = self.find_documents(collection, {"geometry": {"$geoWithin": sphere}}, json_projection)
doc_json = Mongiris.bson_to_json(cursor)
return doc_json
def intersect(self, collection, geometry, json_projection=None):
"""
Find all documents from given collection and which intersect the given geometry
:param collection: the collection to search in
:param geometry: a geojson geometry
:param json_projection: a json document indicating the fields that appear in the results
:return: a cursor (set of documents)
"""
cursor = self.find_documents(collection, {"geometry": {"$geoIntersects": {
"$geometry": geometry}}}, json_projection)
doc_json = Mongiris.bson_to_json(cursor)
return doc_json
@staticmethod
def get_geojson_point(coordinates):
"""
Builds a dictionary with GeoJSON syntax for a point using the given coordinates.
:param coordinates: the coordinates (long, lat) as a list, e.g. [4.8, 45.7]
:return: a dictionary with GeoJSON syntax for a Point
"""
return {"type": "Point", "coordinates": coordinates}
def point_in_which_iris(self, coordinates, json_projection=None):
"""
Find the document (IRIS) containing the given coordinates. Uses near() since geo_within() requires a Polygon.
:param coordinates: an array of coordinates (LONGITUDE, LATITUDE)
:param json_projection: a json document indicating the fields that appear in the results
:return: a json document or None
"""
results = self.near(self.iris_collection, coordinates, json_projection, 1) # distance = 1 meter
if len(results) == 0:
return None
return results[0]
def near(self, collection, coordinates, json_projection=None, distance_max=2000):
"""
Find all documents from given collection and which are near the given geometry (according to distance_max)
:param collection: the collection to search in
:param coordinates: an array of coordinates (LONGITUDE, LATITUDE) - near only accepts Point
:param json_projection: a json document indicating the fields that appear in the results
:param distance_max: the maximum distance of resulting iris, in meters
:return: a cursor (set of documents)
"""
geometry = Mongiris.get_geojson_point(coordinates)
cursor = self.find_documents(collection, {"geometry": {"$near": {
"$geometry": geometry, "$maxDistance": distance_max}}}, json_projection)
doc_json = Mongiris.bson_to_json(cursor)
return doc_json
@staticmethod
def adjacent(collection, geometry, json_projection=None, distance=20, exclude_geometry_iris=None):
"""
Find all adjacent neighbors of an iris represented by geometry.
No adjacent function, so use all coordinates of an iris and find the closest iris (according to distance).
Could be done directly with near(), but near() is less accurate and thus incomplete.
:param collection: the collection to search in
:param geometry: a geojson geometry (Point, Polygon, etc.)
:param json_projection: a json document indicating the fields that appear in the results
:param distance: the maximum distance for an adjacent neighbour, in meters (10 to 50 meters are fine)
:param exclude_geometry_iris: the document _id of the iris represented by geometry, if it needs to be excluded
:return: a cursor (set of documents)
"""
results = list()
results_ids = list()
if exclude_geometry_iris is not None: # to exclude the iris represented by geometry, add it to the results ids
results_ids.append(exclude_geometry_iris)
for coords in geometry["coordinates"][0]:
geometry_coords = Mongiris.get_geojson_point(coords)
cursor = collection.find({"geometry": {"$near": {"$geometry": geometry_coords,
"$maxDistance": distance}}}, json_projection)
for doc in cursor:
doc_id = doc["_id"]
if doc_id not in results_ids: # add the new adjacent iris if not already in the results
results.append(doc)
results_ids.append(doc_id)
doc_json = Mongiris.bson_to_json(results)
return doc_json
if __name__ == "__main__":
print("Run unit tests for testing the mongiris class.")
#convert_geojson_files_to_mongo()
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# =============================================================================
# Unit tests for mongiris
# =============================================================================
import mongiris
import unittest
import random
class TestCase(unittest.TestCase):
def setUp(self):
self.db = mongiris.Mongiris()
def test_count(self):
count = self.db.count_documents(self.db.iris_collection, {})
assert (count == 49403), 'Error: expecting 49403 IRIS to be stored in MongoDB, but stored %i' % count
def test_find_one(self):
iris = self.db.find_one_document(self.db.iris_collection, {"properties.CODE_IRIS": "593500203"})
assert(iris is not None), 'Function get_iris_from_code("593500203") should return one document, not None'
self.db.logger.info(iris)
def test_get_iris_from_code(self):
iris = self.db.get_iris_from_code("593500203")
assert(iris is not None), 'Function find_one_document(..., "593500203") should return one document, not None'
self.db.logger.info(iris)
def test_op_near(self):
random_iris = self.db.get_random_document(self.db.iris_collection)
point_random_iris = random.choice(random_iris["geometry"]["coordinates"][0]) # get a random coordinate
self.db.logger.info("Finding all near IRIS for " + random_iris["properties"]["NOM_IRIS"] + " " +
random_iris["properties"]["CODE_IRIS"])
distance_max = 3000 # in meters
cursor = self.db.near(self.db.iris_collection, point_random_iris, {"properties.NOM_IRIS": 1, "properties.CODE_IRIS": 1},
distance_max)
for doc in cursor:
self.db.logger.info(str(doc["_id"]) + "\t" + doc["properties"]["NOM_IRIS"] + "\t" + doc["properties"]["CODE_IRIS"])
def test_op_intersect(self):
random_iris = self.db.get_random_document(self.db.iris_collection)
geometry_random_iris = random_iris["geometry"]
self.db.logger.info("Finding all intersecting IRIS for " + random_iris["properties"]["NOM_IRIS"] + " " +
random_iris["properties"]["CODE_IRIS"])
cursor = self.db.intersect(self.db.iris_collection, geometry_random_iris, {"properties.NOM_IRIS": 1, "properties.CODE_IRIS": 1})
for doc in cursor:
self.db.logger.info(str(doc["_id"]) + "\t" + doc["properties"]["NOM_IRIS"] + "\t" + doc["properties"]["CODE_IRIS"])
def test_op_adjacent(self):
random_iris = self.db.get_random_document(self.db.iris_collection)
geometry_random_iris = random_iris["geometry"]
self.db.logger.info("Finding all adjacent IRIS for " + random_iris["properties"]["NOM_IRIS"] + " " +
random_iris["properties"]["CODE_IRIS"])
distance_max = 10 # in meters
results = self.db.adjacent(self.db.iris_collection, geometry_random_iris, {"properties.NOM_IRIS": 1, "properties.CODE_IRIS": 1},
distance_max, random_iris["_id"])
for doc in results:
self.db.logger.info(str(doc["_id"]) + "\t" + doc["properties"]["NOM_IRIS"] + "\t" + doc["properties"]["CODE_IRIS"])
def test_op_point_in_which_iris(self):
# test 1
self.db.logger.info("Finding IRIS for (3.685111, 46.514643)")
doc = self.db.point_in_which_iris([3.685111, 46.514643])
assert (doc is not None), 'Coordinates (3.685111, 46.514643) should return one document, not None'
assert (doc["properties"]["CODE_IRIS"] == '031020000'), 'Coordinates (3.685111, 46.514643) should correspond to IRIS 031020000'
self.db.logger.info(str(doc["_id"]) + "\t" + doc["properties"]["NOM_IRIS"] + "\t" + doc["properties"]["CODE_IRIS"])
# test 2
self.db.logger.info("Finding IRIS for (5.685111, 46.514643)")
#sphere = {"$centerSphere": [[5.685111, 46.514643], 5/6378.1 ] }
doc = self.db.point_in_which_iris([5.685111, 46.514643])
assert (doc is not None), 'Coordinates (5.685111, 46.514643) should return one document, not None'
assert (doc["properties"]["CODE_IRIS"] == '391750000'), 'Coordinates (5.685111, 46.514643) should correspond to IRIS 391750000'
self.db.logger.info(str(doc["_id"]) + "\t" + doc["properties"]["NOM_IRIS"] + "\t" + doc["properties"]["CODE_IRIS"])
# test 3
self.db.logger.info("Finding IRIS for (-49.016200, 79.333879)")
doc = self.db.point_in_which_iris([-49.016200, 79.333879])
assert (doc is None), 'Coordinates (79.333879, -49.016200) should return no document (no corresponding IRIS)'
def test_test(self):
coord = [4.8300768, 45.720019]
cursor = self.db.near(self.db.iris_collection, coord, {"properties.NOM_IRIS": 1, "properties.CODE_IRIS": 1},
1000)
for doc in cursor:
self.db.logger.info(str(doc["_id"]) + "\t" + doc["properties"]["NOM_IRIS"] + "\t" + doc["properties"]["CODE_IRIS"])
if __name__ == "__main__":
unittest.main(verbosity=2) # run all tests with verbose mode
\ No newline at end of file
[tool.poetry]
name = "mongiris"
version = "0.17"
description = "This package is an interface for querying INSEE IRIS stored as documents in MongoDB. Requires loading the IRIS files into MongoDB prior to using this package."
authors = ["Duchateau Fabien <fabien.duchateau@univ-lyon1.fr>"]
license = "GPL-3.0"
keywords = ["MongoDB", "INSEE", "IRIS"]
homepage = ""
repository = "https://gitlab.liris.cnrs.fr/fduchate/mongiris.git"
#include = ["vizliris/static/*", "vizliris/templates/*"] # "vizliris/data/*",
[tool.poetry.dependencies]
python = "^3.6"
[tool.poetry.dev-dependencies]
pymongo = "3.7.2"
[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment