Commit e7f6adc2 authored by Adam Paquette's avatar Adam Paquette
Browse files

more clean up and reduction of coverage method

parent fc66ff90
Loading
Loading
Loading
Loading
+27 −0
Original line number Diff line number Diff line
import json

import pandas as pd
from osgeo import ogr

from autocnet.utils import utils
from scipy.spatial import ConvexHull


@@ -77,3 +81,26 @@ def two_poly_overlap(poly1, poly2):
    overlap_area = a_o
    overlap_percn = (a_o / (area1 + area2 - a_o)) * 100
    return overlap_percn, overlap_area


def get_area(poly1, poly2):
    """

    Parameters
    ----------
    poly1 : ogr polygon
            General ogr polygon

    poly2 : ogr polygon
            General ogr polygon

    Returns
    -------
    intersection_area : float
                        returns the intersection area
                        of two polygons

    """
    intersection = poly1.Intersection(poly2)
    intersection_area = intersection.GetArea()
    return intersection_area
+11 −30
Original line number Diff line number Diff line
@@ -384,7 +384,7 @@ class Edge(dict, MutableMapping):
    def plot(self, ax=None, clean_keys=[], **kwargs):
        return plot_edge(self, ax=ax, clean_keys=clean_keys, **kwargs)

    def _clean(self, clean_keys, pid=None):
    def _clean(self, clean_keys=[], pid=None):
        """
        Given a list of clean keys and a provenance id compute the
        mask of valid matches
@@ -426,7 +426,7 @@ class Edge(dict, MutableMapping):
        self.weight['overlap_area'] = overlapinfo[1]
        self.weight['overlap_percn'] = overlapinfo[0]

    def coverage(self, image='source'):
    def coverage(self):
        """
        Acts on the edge given either the source node
        or the destination node and returns the percentage
@@ -448,44 +448,25 @@ class Edge(dict, MutableMapping):
            return
        mask = self.masks.all(axis=1)
        matches = self.matches[mask]
        source_array = self.source.get_keypoint_coordinates(index=matches['source_idx'])
        destination_array = self.destination.get_keypoint_coordinates(index=matches['destination_idx'])

        source_points = np.array(source_array)
        destination_points = np.array(destination_array)
        source_array = self.source.get_keypoint_coordinates(index=matches['source_idx']).values

        source_coords = self.source.geodata.latlon_corners
        destination_coords = self.destination.geodata.latlon_corners

        if image == 'source':
            image_covered = source_points
            node = self.source
        else:
            image_covered = destination_points
            node = self.destination

        # pixel space
        convex_hull = cg.convex_hull(image_covered)
        convex_hull = cg.convex_hull(source_array)

        convex_points = [node.geodata.pixel_to_latlon(row[0], row[1]) for row in convex_hull.points[convex_hull.vertices]]
        convex_points = [self.source.geodata.pixel_to_latlon(row[0], row[1]) for row in convex_hull.points[convex_hull.vertices]]
        convex_coords = [(i, j) for i, j in convex_points]

        # Convert the convex hull pixel coordinates to latlon
        # coordinates
        source_geom = utils.array_to_geom(destination_coords)
        destination_geom = utils.array_to_geom(source_coords)
        convex_geom = utils.array_to_geom(convex_coords)

        source_poly = ogr.CreateGeometryFromJson(json.dumps(source_geom))
        destination_poly = ogr.CreateGeometryFromJson(json.dumps(destination_geom))
        convex_poly = ogr.CreateGeometryFromJson(json.dumps(convex_geom))

        image_intersection = source_poly.Intersection(destination_poly)
        image_intersection_area = image_intersection.GetArea()
        source_poly = utils.array_to_poly(source_coords)
        destination_poly = utils.array_to_poly(destination_coords)
        convex_poly = utils.array_to_poly(convex_coords)

        hull_overlap_poly = image_intersection.Intersection(convex_poly)
        hull_overlap_area = hull_overlap_poly.GetArea()
        intersection_area = cg.get_area(source_poly, destination_poly)

        total_overlap_coverage = (hull_overlap_area/image_intersection_area)
        total_overlap_coverage = (convex_poly.GetArea()/intersection_area)

        return total_overlap_coverage
+6 −2
Original line number Diff line number Diff line
import json

from functools import reduce

import numpy as np
import pandas as pd

from osgeo import ogr

def crossform(a):
    """
@@ -307,7 +310,7 @@ def cartesian(arrays, out=None):
    return out


def array_to_geom(array):
def array_to_poly(array):
    """
    Generate a geojson geom
    Parameters
@@ -329,5 +332,6 @@ def array_to_geom(array):
    geom_array = np.append(array, [array[0]], axis = 0)
    geom_list = np.array(geom_array).tolist()
    geom = {"type": "Polygon", "coordinates": [geom_list]}
    return geom
    poly = ogr.CreateGeometryFromJson(json.dumps(geom))
    return poly