def get_json(points): """ :param points: (long,lat) :return: a count of species and their scientific names """ try: # parse input coordinates longa, lat = float(points.split(",")[0]), float(points.split(",")[1]) i, j = table.grid_cell.array_index(longa, lat, (1, 1)) point = r.points(longa, lat) cell = grid_cells[i][j] result = [] for species in cell.species: par = k.parser(species) if par.inside(point): # check cached results to see if each specie's range map contains this point now = datetime.datetime.now() result.append({ "class": par.class_name, "order": par.order, "family": par.family, "scientific_name": species, "url": par.url, "index_date": str(now) }) result.sort(key=lambda x: x["scientific_name"]) return jsonify(count=len(result), species=result), 200 except: message = """unknown error, try specieslookup.berkeley.edu/about/ for instructions on query formatting""" return message, 400
def inside(self, query_point): # This takes in the point object and checks to see if rangemap intersects the query point if query_point.x < self.min_x or query_point.x > self.max_x or query_point.y < self.min_y or query_point.y > self.max_y: return False a = False b = False for good in self.outer: ps = [r.points(dot[0], dot[1]) for dot in good] poly = r.polygon(ps) if poly.is_inside(query_point): a = True for good in self.inner: ps = [r.points(dot[0], dot[1]) for dot in good] poly = r.polygon(ps) if poly.is_inside(query_point): b = True return a and not b
def get(points): """ :param points: (long,lat) :return: a count of species and their scientific names """ try: # parse input coordinates longa, lat = float(points.split(",")[0]), float(points.split(",")[1]) i, j = table.grid_cell.array_index(longa, lat, (1, 1)) point = r.points(longa, lat) cell = grid_cells[i][j] result = [] for species in cell.species: par = k.parser(species) if par.inside(point): # check cached results to see if each specie's range map contains this point result.append(species) result.append("count: " + str(len(result))) return str(result), 200 except: message = """unknown error, try specieslookup.berkeley.edu/about/ for instructions on query formatting""" return message, 400
import matplotlib from matplotlib.patches import Polygon from matplotlib.collections import PatchCollection import matplotlib.pyplot as plt import numpy as np import ray_casting as r coord = np.array([[0.2, 0.2], [0.4, 0.2], [0.2, 0.9], [0.9, 0.2]]) test = r.points(0.4, 0.9) ps = [r.points(dot[0], dot[1]) for dot in coord] poly = r.polygon(ps) fig, ax = plt.subplots() patches = [] num_polygon = 1 polygon = Polygon(coord, closed=True) patches.append(polygon) p = PatchCollection(patches, cmap=matplotlib.cm.jet, alpha=0.4) ax.add_collection(p) plt.plot([test.x], [test.y], 'ro') plt.title("tested point (" + str(test.x) + " ," + str(test.y) + ") is contained in polygon" + str(poly.is_inside(test))) plt.show()
# This tests the ray_casting module import os import time import kmlparserclass as k import ray_casting as r point = r.points(-7.2, 6) all_species = [name.split('.')[0] for name in next(os.walk('kml'))[1] if name] accoutants = [k.parser(specie) for specie in all_species] start = time.clock() result = [ account.scientific_name for account in accoutants if account.inside(point) ] stop = time.clock() print(stop - start) print(result)
# this is debugging for lookup_table # need to create the class that filters request import kmlparserclass as k from ray_casting import points aspecie = k.parser("Adenomera_ajurauna") point=points(-46.5,-23.825) aspecie.visualize(point) print(aspecie.inside(point))
def format_vertices(arr): return np.array([[float(arr[i][0]), float(arr[i][1])] for i in range(len(arr))]) polys = root.findall(".//{0}Polygon".format(namespace)) for each_polygon in polys: outer = each_polygon.findall(".//{0}outerBoundaryIs".format(namespace)) inner = each_polygon.findall(".//{0}innerBoundaryIs".format(namespace)) for each in outer: cords = find_coordinates(each) good = format_vertices(cords) print(good.shape) ps = [r.points(dot[0], dot[1]) for dot in good] print() poly = r.polygon(ps) test = r.points(-2.8, 6.5) fig, ax = plt.subplots() patches = [] num_polygon = 1 polygon = Polygon(good, closed=True) patches.append(polygon) p = PatchCollection(patches, cmap=matplotlib.cm.jet, alpha=0.4) ax.add_collection(p) plt.plot([test.x], [test.y], 'ro') plt.title("tested point (" + str(test.x) + " ," + str(test.y) +
all_species = [ name.split('.')[0] for name in list(os.walk('range_shapefiles'))[0][2] if name ] start = time.clock() grid_cells = table.create_table() end = time.clock() print(end - start, " seconds for updating grid") # print(grid_cells) points = "-63.043301,-2.217128" count = [[len(cell.species) for cell in row] for row in grid_cells] print(sum([sum(row) for row in count])) start = time.clock() print(points) longa, lat = float(points.split(",")[0]), float(points.split(",")[1]) i, j = table.grid_cell.array_index(longa, lat, (1, 1)) print(i, j) point = r.points(longa, lat) cell = grid_cells[i][j] result = [] for species in cell.species: par = k.parser(species) if par.inside(point): result.append(species) stop = time.clock() print(str(stop - start)) print(result)