diff --git a/analytics.py b/analytics.py index e69de29..6e70791 100644 --- a/analytics.py +++ b/analytics.py @@ -0,0 +1,120 @@ +import math +import sys +import os + + +from .. import point +from .. import utils + +def find_largest_city(gj): + city = None + max_population = 0 + + for i in gj['features']: + if i['properties']['pop_max'] > max_population: + max_population = i['properties']['pop_max'] + city = i['properties']['name'] + + return city, max_population + + +def mean_center(points): + x = 0 + y = 0 + + for i in points: + x += i[0] + y += i[1] + + x /= len(points) + y /= len(points) + + return x, y + + +def average_nearest_neighbor_distance(points): + + mean_d = 0 + nearest_neighbor = math.inf + + for p in points: + for otherPoint in points: + if point.check_coincident(p, otherPoint): + continue + current_distance = utils.euclidean_distance(p, otherPoint) + if nearest_neighbor is None: + nearest_neighbor = current_distance + elif nearest_neighbor > current_distance: + nearest_neighbor = current_distance + + mean_d += nearest_neighbor + nearest_neighbor = None + + mean_d /= len(points) + + return mean_d + + +def minimum_bounding_rectangle(points): + + mbr = [0, 0, 0, 0] + x_min = 0 + x_max = 0 + y_min = 0 + y_max = 0 + + for p in points: + if p[0] < x_min: + x_min = p[0] + if p[0] > x_max: + x_max = p[0] + if p[1] < y_min: + y_min = p[1] + if p[1] > y_max: + y_max = p[1] + mbr = [x_min, y_min, x_max, y_max] + + return mbr + + +def mbr_area(mbr): + + l = mbr[2] - mbr[0] + w = mbr[3] - mbr[1] + area = l * w + + return area + + +def expected_distance(area, n): + + expected = 0.5 * (math.sqrt(area / n)) + + return expected + + +def compute_critical(points): + + lower = min(points) + upper = max(points) + + return lower, upper + + +def check_significant(lower, upper, observed): + + if (lower < observed) or (observed < upper): + result = True + else: + result = False + + return result + + +def permutation(p=99, n=100): + + perm = [] + for x in range(p): + perm.append(average_nearest_neighbor_distance(utils.create_random_points(n))) + + return perm diff --git a/io_geojson.py b/io_geojson.py index e69de29..6beebe1 100644 --- a/io_geojson.py +++ b/io_geojson.py @@ -0,0 +1,10 @@ +import json + + +def read_geojson(input_file): + + with open(input_file, 'r') as f: + gj = json.load(f) + + return gj + diff --git a/point.py b/point.py index e69de29..2c16d96 100644 --- a/point.py +++ b/point.py @@ -0,0 +1,27 @@ +import utils + +sys.path.insert(0, os.path.abspath('..')) + + +def __init__(self, x, y, mark=[]): + self.x = x + self.y = y + self.mark = mark + + +def __str__(self): + return "[0], [1]".format(self.x, self.y) + + +def check_coincident(a, b): + return a == b + + +def shift_point(point, x_shift, y_shift): + x = utils.getx(point) + y = utils.gety(point) + + x += x_shift + y += y_shift + + return x, y diff --git a/utils.py b/utils.py index e69de29..648bc0e 100644 --- a/utils.py +++ b/utils.py @@ -0,0 +1,42 @@ +import math +import random + + +def create_random_points(n): + rand = random.seed() + random_points = [(rand.randint(0, 100), rand.randint(0, 100))] + return random_points + + +def create_random_marked_points(n, marks=[]): + rand_mp = random.seed() + rand_pts = [] + if marks is None: + for i in range(n): + rand_pts.append(rand_mp.randint(0, 100), rand_mp.randint(0,100), rand_mp.choice(marks)) + else: + for i in range(n): + rand_pts.append(rand_mp.randint(0, 100), rand_mp.randint(0,100), rand_mp.choice(marks)) + return rand_pts + + +def euclidean_distance(a, b): + distance = math.sqrt((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2) + return distance + + +def manhattan_distance(a, b): + distance = abs(a[0] - b[0]) + abs(a[1] - b[1]) + return distance + + +def check_in(points, point_list): + return points in point_list + + +def getx(points): + return points[0] + + +def gety(points): + return points[1]