Skip to content

Instantly share code, notes, and snippets.

@lrhache
Last active January 26, 2016 19:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lrhache/1099c53d3ce434c6d576 to your computer and use it in GitHub Desktop.
Save lrhache/1099c53d3ce434c6d576 to your computer and use it in GitHub Desktop.
Neo4j geospatial python
import time
import random
import requests
from profilehooks import profile
def timeit(method):
def timed(*args, **kw):
ts = time.time()
result = method(*args, **kw)
te = time.time()
print '%r (%r, %r) %2.2f sec' % \
(method.__name__, args, kw, te-ts)
return result
return timed
VERSION = ""
INDEX_NAME = 'geo%s' % VERSION
DENVER = (39.73915, -104.9847)
LATITUDE = (38, 41, )
LONGITUDE = (-108, -102, )
AUTH = ('neo4j', 'root')
HEADERS = {
'Content-Type': 'application/json'
}
DB_PATH = 'http://localhost:7474/db/data/'
def get_geo():
return random.uniform(*LATITUDE), random.uniform(*LONGITUDE)
def create_index():
path = DB_PATH + 'ext/SpatialPlugin/graphdb/addSimplePointLayer'
content = {"layer": INDEX_NAME, "lat": "lat", "lon": "lon"}
requests.post(path, json=content, headers=HEADERS, auth=AUTH)
path = DB_PATH + 'index/node'
content = {"name": INDEX_NAME,
"config": {
"provider": "spatial",
"geometry_type": "point",
"lat": "lat",
"lon": "lon"
}}
requests.post(path, json=content, headers=HEADERS, auth=AUTH)
def create_data():
for index in range(1, 100001):
lat, lon = get_geo()
path = DB_PATH + 'node'
content = {"lat": float(lat), "lon": float(lon)}
r = requests.post(path, json=content, headers=HEADERS, auth=AUTH)
resp = r.json()
self = resp['self']
path = resp['labels']
content = ["Location%s" % VERSION]
requests.post(path, json=content, headers=HEADERS, auth=AUTH)
path = DB_PATH + 'index/node/%s' % INDEX_NAME
content = {"key": "dummy", "value": "value", "uri": self}
requests.post(path, json=content, headers=HEADERS, auth=AUTH)
print index
@profile
def test_geo():
path = DB_PATH + 'cypher'
params = {
'lat': float(DENVER[0]),
'lon': float(DENVER[1]),
'distance': float(25)
}
content = {
'query': ("START n=node:geo("
"'withinDistance:[{lat}, {lon}, {distance}]'"
") return n").format(**params),
}
r = requests.post(path, json=content, headers=HEADERS, auth=AUTH)
if __name__ == '__main__':
test_geo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment