-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSpatialAnalysis.py
More file actions
43 lines (32 loc) · 1004 Bytes
/
SpatialAnalysis.py
File metadata and controls
43 lines (32 loc) · 1004 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Script name: SpatialAnalysis.py
Author: NICK
Date: June 2018
Purpose: Used to carryout spatial analysis on data using Shapely
"""
from shapely.geometry import shape
from shapely.geometry import Point
from shapely.geometry import geo
import json
from rtree import index
def ReadGeoJSON(path):
with open(path) as hexFile:
data = json.load(hexFile)
cells = []
for feature in data['features']:
cells.append(shape(feature['geometry']))
return cells
def SpatialJoin(points, polygons):
idx = index.Index()
count = -1
for poly in polygons:
count += 1
idx.insert(count, poly.polygon.bounds)
for i, conc in enumerate(points.concs):
for j in idx.intersection((points.lons[i], points.lats[i])):
if Point(points.lons[i], points.lats[i]).within(polygons[j].polygon):
polygons[j].concs.append(conc)
break
return polygons