Skip to content

Instantly share code, notes, and snippets.

@panchicore
Created September 30, 2016 16:56
Show Gist options
  • Save panchicore/fcee2e3d3b019e1a4f16ceaa20e1d202 to your computer and use it in GitHub Desktop.
Save panchicore/fcee2e3d3b019e1a4f16ceaa20e1d202 to your computer and use it in GitHub Desktop.
import datetime
import requests
# Create boxes from map here:
# http://boundingbox.klokantech.com/
# FORMAT CSV COMES: ymin, xmin, ymax, xmax
PLACES = {
"FRANCE": [
[0.8129882813,45.5563717359,3.9331054688,47.6616878033],
],
"BELGIUM": [
[3.9,50.41,5.38,51.03],
],
"GERMANY": [
[7.7563476563,50.0395018376,10.8764648438,51.9679615872],
# [8.53,47.9,12.74,49.14],
# [7.51,52.24,14.02,53.78],
],
"ITALY": [
[11.0083007813,41.5455890367,14.1284179687,43.7988540272],
],
"POLAND": [
[17.6879882813,51.1552316116,20.8081054688,53.0379102021],
],
"SPAIN": [
[-5.4272460938,39.1343212453,-2.3071289063,41.4715443871],
],
"ALGERIA": [
[1.6918945313,26.7112669135,4.8120117188,29.4156754712],
],
"UK": [
[-3.0541992187,51.5941352786,0.0659179688,53.4586199114],
],
"IRELAND": [
[-9.4702148437,52.2984017516,-6.3500976562,54.1334781429],
],
}
PLACES_R = {
"RANDOM": [
[0.4614257813,45.9511496867,3.1640625,47.6616878033]
]
}
PLACES = []
x = [0.4614257813,45.9511496867,3.1640625,47.6616878033]
for i in range(1, 20):
PLACES.append(
[
x[0] - i,
x[1] - i,
x[2] + i,
x[3] + i
]
)
PLACES = {"RANDOM": PLACES}
ZONE = []
def add():
layers = []
layer_id = 1
for name, coords in PLACES.iteritems():
for i, coor in enumerate(coords, 1):
minY = coor[1]
minX = coor[0]
maxY = coor[3]
maxX = coor[2]
layer = {
"id": layer_id,
"type": 'Layer',
"name": name,
"title": "%s %i" % (name, i),
"abstract": "Lorem ipsum",
"min_y": minY,
"min_x": minX,
"max_x": maxX,
"max_y": maxY,
"layer_date": datetime.datetime.today().isoformat(),
"layer_geoshape": {
"type": "envelope",
"coordinates": [
[minX, maxY], [maxX, minY]
]
},
}
layers.append(layer)
layer_id += 1
mapping = {
"mappings": {
"layer": {
"properties": {
"layer_geoshape": {
"type": "geo_shape",
"tree": "quadtree",
"precision": "200m"
}
}
}
}
}
ES_URL = "http://localhost:9200/hypermap"
print "> Delete indice..."
requests.delete(ES_URL)
print "> Create indice..."
requests.put(ES_URL, json=mapping)
print "> Adding layers..."
for layer in layers:
url = "{0}/layer/{1}".format(ES_URL, layer["id"])
res = requests.post(url, json=layer)
if not res.ok:
print res.json()
print "> Done"
if __name__ == '__main__':
add()
"""
27.
[10.866413,-74.943779 TO 11.224420,-74.563119]
http://localhost:8000/registry/hypermap/api/?q.time=%5B1900-01-01%20TO%202016-12-31T00%3A00%3A00%5D&q.geo=%5B10.866413%2C-74.943779%20TO%2011.224420%2C-74.563119%5D&d.docs.limit=0&d.docs.page=1&d.docs.sort=score&a.time.limit=0&a.time.gap=P1D&a.hm.limit=0&a.hm.gridlevel=0&a.text.limit=0&a.user.limit=0&original_response=0
"""
import datetime
import requests
from estests import PLACES
def main():
layers = []
layer_id = 1
for name, coords in PLACES.iteritems():
for i, coor in enumerate(coords, 1):
minY = coor[1]
minX = coor[0]
maxY = coor[3]
maxX = coor[2]
wkt = "ENVELOPE({:f},{:f},{:f},{:f})".format(minX, maxX, maxY, minY)
layer = {
"id": layer_id,
"type": 'Layer',
"name": name,
"title": "%s %i" % (name, i),
"abstract": "Lorem ipsum",
"min_y": minY,
"min_x": minX,
"max_x": maxX,
"max_y": maxY,
"layer_date": "2013-12-02T00:00:00Z",
"bbox": wkt
}
layers.append(layer)
layer_id += 1
SOLR_URL = "http://localhost:8983/solr"
print "> Creating scheme..."
schema_url = "{0}/hypermap/schema".format(SOLR_URL)
fields = [
{"name": "abstract", "type": "string"},
{"name": "area", "type": "tdouble"},
{"name": "availability", "type": "string"},
{"name": "bbox", "type": "location_rpt"},
{"name": "domain_name", "type": "string"},
{"name": "id", "type": "tlong", "required": True},
{"name": "is_public", "type": "boolean"},
{"name": "last_status", "type": "boolean"},
{"name": "layer_date", "type": "tdate", "docValues": True},
{"name": "layer_datetype", "type": "string"},
{"name": "layer_id", "type": "tlong"},
{"name": "layer_originator", "type": "string"},
{"name": "location", "type": "string"},
{"name": "max_x", "type": "tdouble"},
{"name": "max_y", "type": "tdouble"},
{"name": "min_x", "type": "tdouble"},
{"name": "min_y", "type": "tdouble"},
{"name": "name", "type": "string"},
{"name": "recent_reliability", "type": "tdouble"},
{"name": "reliability", "type": "tdouble"},
{"name": "service_id", "type": "tlong"},
{"name": "service_type", "type": "string"},
{"name": "srs", "type": "string", "multiValued": True},
{"name": "tile_url", "type": "string"},
{"name": "title", "type": "string"},
{"name": "type", "type": "string"},
{"name": "url", "type": "string"},
{"name": "layer_username", "type": "string"},
{"name": "layer_category", "type": "string"},
{"name": "centroid_y", "type": "tdouble"},
{"name": "centroid_x", "type": "tdouble"},
]
headers = {
"Content-type": "application/json"
}
for field in fields:
data = {
"add-field": field
}
requests.post(schema_url, json=data, headers=headers)
url_solr_update = '{0}/hypermap/update/json/docs'.format(SOLR_URL)
headers = {"content-type": "application/json"}
params = {"commitWithin": 1500}
print "> Clearing core..."
requests.get("{0}/hypermap/update?stream.body=<delete><query>*:*</query></delete>&commit=true".format(SOLR_URL))
print "> Adding layers..."
for layer in layers:
res = requests.post(url_solr_update, json=layer, params=params, headers=headers)
if res.json().get("responseHeader").get("status") != 0:
print res.json()
print "> Done."
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment