Commit d394aaec authored by kberry's avatar kberry
Browse files

Basic 'calculate overlap' funcationality working (maybe) on cubes

parent 14fcc130
Loading
Loading
Loading
Loading
+39 −6
Original line number Diff line number Diff line
import pvl
import os
import warnings

from pysal.cg.shapes import Polygon
from autocnet.utils.utils import find_in_dict, find_nested_in_dict
from osgeo import ogr
import numpy as np
import gdal
import osr
@@ -191,12 +195,35 @@ class GeoDataset(object):
    @property
    def latlon_extent(self):
        if not getattr(self, '_latlon_extent', None):
            try:
                xy_extent = self.xy_extent
                lowerlat, lowerlon = self.pixel_to_latlon(xy_extent[0][0], xy_extent[0][1])
                upperlat, upperlon = self.pixel_to_latlon(xy_extent[1][0], xy_extent[1][1])
                self._latlon_extent = [(lowerlat, lowerlon), (upperlat, upperlon)]
            except:
                warnings.warn("Couldn't calculate a Latitude/Longitude extent")
                self._latlon_extent = None

        return self._latlon_extent

    @property
    def footprint_polygon(self):
        if not getattr(self, '_pvl_header', None):
            self._pvl_header = pvl.load(self.file_name) # Should be a member variable?
            polygon_pvl = find_in_dict(self._pvl_header, 'Polygon')
            start_polygon_byte = find_in_dict(polygon_pvl, 'StartByte')
            num_polygon_bytes = find_in_dict(polygon_pvl, 'Bytes')

            # TODO: Do we really need to re-open the file here? self.dataset does not work
            f = open(self.file_name, 'r+')
            f.seek(start_polygon_byte - 1)
            wkt = f.read(num_polygon_bytes)
            polygon = ogr.CreateGeometryFromWkt(wkt)
            self._footprint_polygon = polygon
            f.close()
        return self._footprint_polygon


    @property
    def xy_extent(self):
        if not getattr(self, '_xy_extent', None):
@@ -220,8 +247,14 @@ class GeoDataset(object):

        """
        if not getattr(self, '_bounding_box', None):
            try:
                latlons = self.latlon_extent # Will fail without geospatial data
                self._bounding_box = cg.standalone.get_bounding_box([cg.shapes.LineSegment(latlons[0], latlons[1])])
                print(list(self._bounding_box))
            except:
                envelope = self.footprint_polygon.GetEnvelope()
                self._bounding_box = [envelope[0], envelope[2], envelope[1], envelope[3]] # TODO: check me?
                print(self.bounding_box)
        return self._bounding_box

    @property
+14 −0
Original line number Diff line number Diff line
@@ -76,6 +76,20 @@ class TestFromList(unittest.TestCase):

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

class TestFromListCubes(unittest.TestCase):
    @classmethod

    def setUpClass(cls):
        filelist = [get_path('AS15-M-0297_sub4.cub'),
                    get_path('AS15-M-0298_sub4.cub'),
                    get_path('AS15-M-0299_sub4.cub')]
        cls.graph = network.CandidateGraph.from_filelist(filelist)

    def test_graph_length(self):
        self.assertEqual(self.graph.number_of_nodes(), 3)
        self.assertEqual(self.graph.number_of_edges(), 3)

class TestEdge(unittest.TestCase):