Commit 7c42695d authored by Adam Paquette's avatar Adam Paquette
Browse files

First steps in method implimentation

parent 591b6cef
Loading
Loading
Loading
Loading
+64 −0
Original line number Diff line number Diff line
import json
import ogr
import pandas as pd
from scipy.spatial import ConvexHull


@@ -45,3 +46,66 @@ def overlapping_polygon_area(polys):
        intersection = intersection.Intersection(geom)
    area = intersection.GetArea()
    return area


def convex_hull(points):

    """

    Parameters
    ----------
    points : ndarray
             (n, 2) array of point coordinates

    Returns
    -------
    hull_poly : ogr
             an ogr polygon that is built out of
             the convex_hull

    """

    points.pixel_to_latlon()

    if isinstance(points, pd.DataFrame) :
        points = pd.DataFrame.as_matrix(points)

    hull = ConvexHull(points)
    coordinates = [[i,j] for (i, j) in hull.points]
    geom = {"type": "Polygon", "coordinates": [coordinates]}
    hull_poly = ogr.CreateGeometryFromJson(json.dumps(geom))
    return hull_poly

def two_poly_overlap(poly1, poly2):
    """

    Parameters
    ----------
    poly1 : ogr polygon
            Any polygon that shares some kind of overlap
            with poly2

    poly2 : ogr polygon
            Any polygon that shares some kind of overlap
            with poly1

    Returns
    -------
     overlap_info : list
            The ratio convex hull volume / ideal_area

    """
    a_o = poly1.Intersection(poly2).GetArea()
    area1 = poly1.GetArea()
    area2 = poly2.GetArea()

    overlap_area = a_o
    overlap_percn = (a_o / (area1 + area2 - a_o)) * 100
    overlap_info = [overlap_percn, overlap_area]
    return overlap_info

def hull_overlap(poly1, poly2, convex_poly):
    a_o = poly1.Intersection(poly2)
    convex_overlap = convex_poly.Intersection(a_o)
    point_coverage = (convex_overlap.GetArea()/a_o.GetArea())*100
    return point_coverage
 No newline at end of file
+14 −15
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ from autocnet.matcher import suppression_funcs as spf
from autocnet.matcher import subpixel as sp
from autocnet.transformation.transformations import FundamentalMatrix, Homography
from autocnet.vis.graph_view import plot_edge
from autocnet.cg import cg


class Edge(dict, MutableMapping):
@@ -87,21 +88,6 @@ class Edge(dict, MutableMapping):
    def health(self):
        return self._health.health

    def overlap(self):
        if hasattr(self, 'weight'):
            poly1 = self.source.geodata.footprint
            poly2 = self.destination.geodata.footprint

            intersection = poly1.Intersection(poly2)
            self._overlap_area = intersection.GetArea()

            total_area = poly1.GetArea()
            intersection_area = intersection.GetArea()
            self._overlap_percn = (intersection_area/total_area)*100

            self.weight['overlap_area'] = self._overlap_area
            self.weight['overlap_percn'] = self._overlap_percn

    def symmetry_check(self):
        if hasattr(self, 'matches'):
            mask = od.mirroring_test(self.matches)
@@ -432,3 +418,16 @@ class Edge(dict, MutableMapping):
            mask = pd.Series(True, self.matches.index)

        return matches, mask

    def overlap(self):
        poly1 = self.source.geodata.footprint
        poly2 = self.destination.geodata.footprint

        self._overlapinfo = cg.two_poly_overlap(poly1, poly2)
        hull_poly = cg.convex_hull(self.source.get_keypoint_coordinates())
        self._convex_overlap = cg.hull_overlap(poly1, poly2, hull_poly)

        self.weight['overlap_area'] = self._overlapinfo[1]
        self.weight['overlap_percn'] = self._overlapinfo[0]
        self.weight['overlap_coverage'] = self._convex_overlap
+4 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@ from autocnet.graph.edge import Edge
from autocnet.graph.node import Node
from autocnet.matcher.matcher import FlannMatcher
from autocnet.vis.graph_view import plot_graph
from autocnet.cg import cg


class CandidateGraph(nx.Graph):
@@ -476,6 +477,9 @@ class CandidateGraph(nx.Graph):
        '''
        self.apply_func_to_edges('suppress', *args, **kwargs)

    def overlap(self, *args, **kwargs):
        self.apply_func_to_edges('overlap')

    def minimum_spanning_tree(self):
        """
        Calculates the minimum spanning tree of the graph