Commit dc0115f0 authored by Tomas Pettersson's avatar Tomas Pettersson 🏸

activated onland again now reading json instead of pickle

parent 558bcf75
2017-12-13 08:00 27.0119 60.0031 0.0000
2017-12-13 08:15 27.0113 60.0034 0.0000
2017-12-13 08:30 27.0103 60.0036 0.0000
2017-12-13 08:45 27.0094 60.0039 0.0000
2017-12-13 09:00 27.0083 60.0043 0.0000
2017-12-13 08:30 27.0102 60.0037 0.0000
2017-12-13 08:45 27.0094 60.0040 0.0000
2017-12-13 09:00 27.0084 60.0043 0.0000
2017-12-13 09:15 27.0075 60.0046 0.0000
2017-12-13 09:30 27.0065 60.0052 0.0000
2017-12-13 09:30 27.0064 60.0052 0.0000
2017-12-13 09:45 27.0059 60.0057 0.0000
2017-12-13 10:00 27.0052 60.0061 0.0000
2017-12-13 10:00 27.0053 60.0061 0.0000
2017-12-13 10:15 27.0047 60.0066 0.0000
2017-12-13 10:30 27.0041 60.0072 0.0000
2017-12-13 10:30 27.0040 60.0072 0.0000
2017-12-13 10:45 27.0034 60.0077 0.0000
2017-12-13 11:00 27.0028 60.0082 0.0000
2017-12-13 11:15 27.0025 60.0088 0.0000
2017-12-13 11:30 27.0022 60.0093 0.0000
2017-12-13 11:45 27.5613 59.4504 0.0000
2017-12-13 12:00 32.4847 54.5275 0.0000
2017-12-13 12:15 40.9888 46.0239 0.0000
2017-12-13 12:30 50.3883 36.6248 0.0000
2017-12-13 12:45 58.3332 28.6800 0.0000
2017-12-13 11:45 27.6732 59.3385 0.0000
2017-12-13 12:00 32.3728 54.6394 0.0000
2017-12-13 12:15 40.2055 46.8072 0.0000
2017-12-13 12:30 50.2764 36.7366 0.0000
2017-12-13 12:45 58.5570 28.4562 0.0000
2017-12-13 13:00 60.0117 27.0015 0.0000
2017-12-13 13:15 60.0117 27.0015 0.0000
2017-12-13 13:30 60.0117 27.0015 0.0000
......
......@@ -4,6 +4,7 @@
import os
import sys
import json
import codecs
import pickle
import pkg_resources
from traceback import print_exc
......@@ -21,31 +22,58 @@ def storetree():
lines = coastlinefile.readlines()
section = []
sections = []
jsonsections = []
for line in lines:
# if len(section) == 1000:
# sections.append(LineString(section))
# section = []
if len(section) == 10:
sections.append(LineString(section))
jsonsections.append(section)
section = []
if '-999999' in line:
if len(section) > 0: #this will skip some points where section will end with only one coord. Possibly create a line from previous section on ending point
if len(section) > 1: #this will skip some points where section will end with only one coord. Possibly create a line from previous section on ending point
sections.append(LineString(section))
jsonsections.append(section)
section = []
continue
split = line.split(' ')
section.append([float(split[2]),float(split[1])])
print('Number of lines: '+str(len(sections)))
with open('resources/strtree.pickle', 'wb') as handle:
pickle.dump(sections, handle, protocol=pickle.HIGHEST_PROTOCOL)
# verify
with open('resources/strtree.pickle', 'wb') as dumphandle:
print('HIGHEST_PROTOCOL :'+str(pickle.HIGHEST_PROTOCOL))
pickle.dump(sections, dumphandle, protocol=pickle.HIGHEST_PROTOCOL)
with codecs.open('resources/strtree.json', 'w', 'utf8') as jsonhandle:
jsonhandle.write(json.dumps(jsonsections, sort_keys = True, ensure_ascii=False))
except Exception as error:
print_exc()
sys.exit(1)
def loadtree():
try:
with open('resources/strtree.pickle', 'rb') as picklehandle:
# tree = STRtree(pickle.load(pkg_resources.resource_stream(__name__, '/'.join(('resources', 'strtree.pickle')))))
tree = STRtree(pickle.load(picklehandle))
queryline = LineString([[57.5,17.5],[57.5,18.5]])
matches = tree.query(queryline);
print('Number of pickle matches: '+str(len(matches)))
with open('resources/strtree.json', 'r') as jsonhandle:
#jsondata = json.load(pkg_resources.resource_stream(__name__, '/'.join(('resources', 'strtree.json'))));
jsondata = json.load(jsonhandle);
sections = []
for section in jsondata:
sections.append(LineString(section))
tree = STRtree(sections)
queryline = LineString([[57.5,17.5],[57.5,18.5]])
matches = tree.query(queryline);
print('Number of matches: '+str(len(matches)))
print('Number of json matches: '+str(len(matches)))
except Exception as error:
print_exc()
sys.exit(1)
# # tree = STRtree(sections)
def read_properties(filepath, sep='=', comment_char='#'):
props = {}
......@@ -78,7 +106,13 @@ def writeStatus(status):
def run():
try:
writeStatus("INITIAL")
strtree = STRtree(pickle.load(pkg_resources.resource_stream(__name__, '/'.join(('resources', 'strtree.pickle')))))
# strtree = STRtree(pickle.load(pkg_resources.resource_stream(__name__, '/'.join(('resources', 'strtree.pickle')))))
jsondata = json.load(pkg_resources.resource_stream(__name__, '/'.join(('resources', 'strtree.json'))));
sections = []
for section in jsondata:
sections.append(LineString(section))
strtree = STRtree(sections)
m = Model()()
with open('input.json',"r") as inputfile:
print("PARTIAL")
......@@ -114,5 +148,6 @@ def run():
To run in terminal call with python __init__.py
'''
if __name__ == "__main__":
storetree()
#storetree()
loadtree()
\ No newline at end of file
......@@ -286,21 +286,20 @@ class Model(object):
linestring = geometry.shape(exercisefeature['geometry'])
latlngpoints = [[latlng[1], latlng[0]] for latlng in linestring.coords]
# print('before matches')
# matches = strtree.query(LineString(latlngpoints))
matches = strtree.query(LineString(latlngpoints))
# print('matches: '+str(matches))
for i, coord in enumerate(linestring.coords):
# start = time.time()
# if (len(mp.geoms) > 0):
start = time.time()
if (len(mp.geoms) > 0):
points = self.centerPoints(coord, mp.centroid, mp.geoms)
displacedpoints = self.displacePoints(pDist, points)
# activepoints, onlandpoints = self.onlandPoints(mp.geoms, displacedpoints, matches)
# deactivepoints += onlandpoints
activepoints, onlandpoints = self.onlandPoints(mp.geoms, displacedpoints, matches)
deactivepoints += onlandpoints
# print('active: '+str(len(activepoints)))
# print('onland: '+str(len(onlandpoints)))
# end = time.time()
end = time.time()
# print('onland: '+str(end-start))
# mp = geometry.MultiPoint(activepoints+deactivepoints)
mp = geometry.MultiPoint(displacedpoints)
mp = geometry.MultiPoint(activepoints+deactivepoints)
level = [0] * len(mp.geoms)
category = [2] * len(mp.geoms)
properties = self.createProperties((i+2), timearray[i], mp.centroid, level, category)
......@@ -308,7 +307,7 @@ class Model(object):
if (i % 10 == 0):
Cloudtrack.write(featurecollection)
Particletrack.write(featurecollection)
# mp = geometry.MultiPoint(activepoints)
mp = geometry.MultiPoint(activepoints)
print('deactivepoints: '+str(len(deactivepoints)))
......
This diff is collapsed.
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