Commit 8c73cfc6 authored by jlaura's avatar jlaura
Browse files

Merge pull request #100 from Kelvinrr/intersection_fail

added robustness to poor footprints
parents 5a69711f 9bcf3c4f
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -22,3 +22,12 @@ def delete_dir(dir):
          Remove a directory
    """
    shutil.rmtree(dir)


def file_to_list(file):
    with open(file, 'r') as f:
        file_list = f.readlines()
        file_list = map(str.rstrip, file_list)
        file_list = filter(None, file_list)

    return file_list
+22 −13
Original line number Diff line number Diff line
@@ -5,8 +5,10 @@ import dill as pickle
import networkx as nx
import numpy as np
import pandas as pd
import warnings

from autocnet.fileio.io_gdal import GeoDataset
from autocnet.fileio import io_utils
from autocnet.fileio import io_hdf
from autocnet.control.control import C
from autocnet.fileio import io_json
@@ -107,11 +109,8 @@ class CandidateGraph(nx.Graph):
        : object
          A Network graph object
        """
        if not isinstance(filelist, list):
            with open(filelist, 'r') as f:
                filelist = f.readlines()
                filelist = map(str.rstrip, filelist)
                filelist = filter(None, filelist)
        if isinstance(filelist, str):
            filelist = io_utils.file_to_list(filelist)

        # TODO: Reject unsupported file formats + work with more file formats
        if basepath:
@@ -121,23 +120,28 @@ class CandidateGraph(nx.Graph):

        # This is brute force for now, could swap to an RTree at some point.
        adjacency_dict = {}
        valid_datasets = []

        for i, j in itertools.permutations(datasets,2):
            if not i.file_name in adjacency_dict.keys():
        for i in datasets:
            adjacency_dict[i.file_name] = []
            if not j.file_name in adjacency_dict.keys():
                adjacency_dict[j.file_name] = []

            fp = i.footprint
            if fp and fp.IsValid():
                valid_datasets.append(i)
            else:
                warnings.warn('Missing or invalid geospatial data for {}'.format(i.base_name))

        # Grab the footprints and test for intersection
        for i, j in itertools.permutations(valid_datasets, 2):
            i_fp = i.footprint
            j_fp = j.footprint

            try:
                if j_fp and i_fp and i_fp.Intersects(j_fp):
                if i_fp.Intersects(j_fp):
                    adjacency_dict[i.file_name].append(j.file_name)
                    adjacency_dict[j.file_name].append(i.file_name)
            except:
                warnings.warn('No or incorrect geospatial information for {} and/or {}'.format(i, j))
                warnings.warn('Failed to calculated intersection between {} and {}'.format(i, j))

        return cls(adjacency_dict)

@@ -300,6 +304,11 @@ class CandidateGraph(nx.Graph):
            descriptors = node.descriptors
            # Load the neighbors of the current node into the FLANN matcher
            neighbors = self.neighbors(i)

            # if node has no neighbors, skip
            if not neighbors:
                continue

            for n in neighbors:
                neighbor_descriptors = self.node[n].descriptors
                self._fl.add(neighbor_descriptors, n)
+22 −0
Original line number Diff line number Diff line
@@ -4,9 +4,16 @@ sys.path.insert(0, os.path.abspath('..'))

import unittest

from unittest.mock import patch
from unittest.mock import PropertyMock
from unittest.mock import MagicMock
from osgeo import ogr
import gdal

import numpy as np

from autocnet.examples import get_path
from autocnet.fileio import io_gdal

from .. import network

@@ -100,6 +107,21 @@ class TestCandidateGraph(unittest.TestCase):
    def test_fromlist(self):
        mock_list = ['AS15-M-0295_SML.png', 'AS15-M-0296_SML.png', 'AS15-M-0297_SML.png',
                     'AS15-M-0298_SML.png', 'AS15-M-0299_SML.png', 'AS15-M-0300_SML.png']

        good_poly = ogr.CreateGeometryFromWkt('POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))')
        bad_poly = ogr.CreateGeometryFromWkt('POLYGON ((9999 10, 40 40, 20 40, 10 20, 30 10))')

        with patch('autocnet.fileio.io_gdal.GeoDataset.footprint', new_callable=PropertyMock) as patch_fp:
            patch_fp.return_value = good_poly
            n = network.CandidateGraph.from_filelist(mock_list, get_path('Apollo15'))
            self.assertEqual(n.number_of_nodes(), 6)
            self.assertEqual(n.number_of_edges(), 15)

            patch_fp.return_value = bad_poly
            n = network.CandidateGraph.from_filelist(mock_list, get_path('Apollo15'))
            self.assertEqual(n.number_of_nodes(), 6)
            self.assertEqual(n.number_of_edges(), 0)

        n = network.CandidateGraph.from_filelist(mock_list, get_path('Apollo15'))
        self.assertEqual(len(n.nodes()), 6)