Commit 3dccfd04 authored by jlaura's avatar jlaura
Browse files

Merge pull request #56 from kree/adjacency

Create a graph from a filelist (calculate adjacency structure)
parents 27d63fe9 14fcc130
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
AS15-M-0297_SML.png
AS15-M-0298_SML.png
AS15-M-0299_SML.png
+17 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ import gdal
import osr

from autocnet.fileio import extract_metadata
from pysal import cg

gdal.UseExceptions()

@@ -44,6 +45,9 @@ class GeoDataset(object):
    base_name : str
                The base name of the input image, extracted from the full path.

    bounding_box : object
                 The bounding box of the image in lat/lon space

    geotransform : object
                   Geotransform reference OGR object as an array of size 6 containing the affine 
                   transformation coefficients for transforming from raw sample/line to projected x/y.
@@ -207,6 +211,19 @@ class GeoDataset(object):

        return self._xy_extent

    @property
    def bounding_box(self):
        """
        A bounding box in lat/lon space
        Returns
        -------

        """
        if not getattr(self, '_bounding_box', None):
            latlons = self.latlon_extent # Will fail without geospatial data
            self._bounding_box = cg.standalone.get_bounding_box([cg.shapes.LineSegment(latlons[0], latlons[1])])
        return self._bounding_box

    @property
    def pixel_polygon(self):
        """
+42 −2
Original line number Diff line number Diff line
@@ -5,7 +5,9 @@ import networkx as nx
import numpy as np
import pandas as pd


from pysal import cg
from autocnet.examples import get_path
from autocnet.fileio.io_gdal import GeoDataset
from autocnet.control.control import C
from autocnet.fileio import io_json
from autocnet.matcher.matcher import FlannMatcher
@@ -77,6 +79,44 @@ class CandidateGraph(nx.Graph):
            graph = pickle.load(f)
        return graph

# TODO: Add ability to actually read this out of a file?
    @classmethod
    def from_filelist(cls, filelst):
        """
        Instantiate the class using a filelist as a python list.
        An adjacency structure is calculated using the lat/lon information in the
        input images. Currently only images with this information are supported.

        Parameters
        ----------
        filelst : list
                  A list containing the files (with full paths) to construct an adjacency graph from

        Returns
        -------
        : object
          A Network graph object
        """

        # TODO: Reject unsupported file formats + work with more file formats

        dataset_list = []
        for file in filelst:
            dataset = GeoDataset(file)
            dataset_list.append(dataset)

        adjacency_dict = {}
        for data in dataset_list:
            adjacent_images = []
            other_datasets = dataset_list.copy()
            other_datasets.remove(data)
            for other in other_datasets:
                if(cg.standalone.bbcommon(data.bounding_box, other.bounding_box)):
                    adjacent_images.append(other.base_name)
            adjacency_dict[data.base_name] = adjacent_images
        return cls(adjacency_dict)


    @classmethod
    def from_adjacency(cls, input_adjacency, basepath=None):
        """
@@ -105,7 +145,7 @@ class CandidateGraph(nx.Graph):
                for k, v in input_adjacency.items():
                    input_adjacency[k] = [os.path.join(basepath, i) for i in v]
                    input_adjacency[os.path.join(basepath, k)] = input_adjacency.pop(k)

#        print(input_adjacency)
        return cls(input_adjacency)

    def get_name(self, node_index):
+11 −1
Original line number Diff line number Diff line
@@ -66,10 +66,20 @@ class TestCandidateGraph(unittest.TestCase):
        pass


class TestFromList(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        filelist = [get_path('Mars_MGS_MOLA_ClrShade_MAP2_0.0N0.0_MERC.tif'),
                    get_path('Lunar_LRO_LOLA_Shade_MAP2_90.0N20.0_LAMB.tif'),
                    get_path('Mars_MGS_MOLA_ClrShade_MAP2_90.0N0.0_POLA.tif')]
        cls.graph = network.CandidateGraph.from_filelist(filelist)

    def test_graph_length(self):
        self.assertEqual(self.graph.__len__(), 3)

class TestEdge(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.graph = network.CandidateGraph.from_adjacency(get_path('adjacency.json'))