Пример #1
0
 def test_sql_subset(self):
     sc = GeomCabinet()
     path = sc.get_shp_path('state_boundaries')
     ds = ogr.Open(path)
     ret = ds.ExecuteSQL('select * from state_boundaries where state_name = "New Jersey"')
     ret.ResetReading()
     self.assertEqual(len(ret), 1)
Пример #2
0
 def test_get_keys(self, path=None):
     if path is not None:
         env.DIR_GEOMCABINET = path
     sc = GeomCabinet()
     ret = sc.keys()
     target_keys = ['state_boundaries']
     self.assertEqual(len(set(target_keys).intersection(set(ret))), len(target_keys))
Пример #3
0
    def test_multivariate_iteration(self):
        field = self.get_field(with_value=True, month_count=1)
        field.variables.add_variable(Variable(value=field.variables['tmax'].value + 5,
                                              name='tmin', alias='tmin'))
        field.temporal.name_uid = 'tid'
        field.level.name_uid = 'lid'
        field.spatial.geom.name_uid = 'gid'

        div = Divide(field=field, parms={'arr1': 'tmin', 'arr2': 'tmax'}, alias='some_division',
                     dtype=np.float64)
        ret = div.execute()

        cfield = DerivedMultivariateField(variables=ret, realization=field.realization, temporal=field.temporal,
                                          level=field.level,
                                          spatial=field.spatial, meta=field.meta, uid=field.uid)
        cfield.spatial.name_uid = 'gid'

        sc = GeomCabinet()
        meta = sc.get_meta('state_boundaries')
        sp = SpatialCollection(meta=meta, key='state_boundaries', headers=constants.HEADERS_MULTI)
        for row in sc.iter_geoms('state_boundaries', as_spatial_dimension=True):
            sp.add_field(cfield, ugeom=row)

        for ii, row in enumerate(sp.get_iter_dict(melted=True)):
            if ii == 0:
                self.assertDictEqual(row[1], {'lid': 1, 'ugid': 1, 'cid': 1, 'did': None, 'year': 2000,
                                              'time': datetime.datetime(2000, 1, 1, 12, 0),
                                              'calc_alias': 'some_division', 'value': 12.989774984574424, 'month': 1,
                                              'gid': 1, 'calc_key': 'divide', 'tid': 1, 'level': 50, 'day': 1})
        self.assertEqual(ii + 1, 2 * 31 * 2 * 3 * 4 * 51)
Пример #4
0
 def test_iter_geoms_no_load_geoms(self):
     sc = GeomCabinet()
     it = sc.iter_geoms('state_boundaries', load_geoms=False)
     geoms = list(it)
     self.assertEqual(len(geoms), 51)
     self.assertEqual(geoms[12]['properties']['STATE_NAME'], 'New Hampshire')
     for geom in geoms:
         self.assertNotIn('geom', geom)
Пример #5
0
 def get_collection(self):
     field = self.get_field(with_value=True)
     sc = GeomCabinet()
     meta = sc.get_meta('state_boundaries')
     sp = SpatialCollection(meta=meta, key='state_boundaries')
     for row in sc.iter_geoms('state_boundaries', as_spatial_dimension=True):
         sp.add_field(field, ugeom=row)
     return sp
Пример #6
0
    def test_init(self):
        bp = '/a/bad/location'
        with self.assertRaises(ValueError):
            cabinet = GeomCabinet(bp)
            list(cabinet.iter_geoms('state_boundaries'))

        try:
            ocgis.env.set_geomcabinet_path(None)
            with self.assertRaises(ValueError):
                list(GeomCabinet().iter_geoms('state_boundaries'))
        finally:
            ocgis.env.reset()
Пример #7
0
    def test_number_in_shapefile_name(self):
        """Test number in shapefile name."""

        sc = GeomCabinet()
        path = sc.get_shp_path('state_boundaries')
        out_path = os.path.join(self.current_dir_output, '51_states.shp')
        with fiona.open(path) as source:
            with fiona.open(out_path, mode='w', driver='ESRI Shapefile', schema=source.meta['schema'],
                            crs=source.meta['crs']) as sink:
                for record in source:
                    sink.write(record)
        ret = list(GeomCabinetIterator(select_uid=[23], path=out_path))
        self.assertEqual(len(ret), 1)
Пример #8
0
    def test_iter_geoms(self):
        sc = GeomCabinet()
        it = sc.iter_geoms('state_boundaries')
        geoms = list(it)
        self.assertEqual(len(geoms), 51)
        self.assertEqual(geoms[12]['properties']['STATE_NAME'], 'New Hampshire')
        for geom in geoms:
            self.assertIn(type(geom['geom']), (Polygon, MultiPolygon))

        # test with a shapefile not having a unique identifier
        env.DEFAULT_GEOM_UID = 'ggidd'
        new = self.get_shapefile_path_with_no_ugid()
        sc = GeomCabinet()
        target = list(sc.iter_geoms(path=new))
        self.assertEqual(len(target), 11)
        self.assertEqual(target[0]['properties'][env.DEFAULT_GEOM_UID], 1)
        self.assertEqual(target[3]['properties'][env.DEFAULT_GEOM_UID], 4)

        target = list(sc.iter_geoms(path=new, uid='ID'))
        self.assertNotIn(env.DEFAULT_GEOM_UID, target[9]['properties'])
        self.assertEqual(int, type(target[7]['properties']['ID']))

        target = list(sc.iter_geoms(path=new, uid='ID', as_spatial_dimension=True))
        ref = target[4]
        self.assertEqual(ref.uid[0, 0], 10)
        self.assertNotIn(env.DEFAULT_GEOM_UID, ref.properties.dtype.names)

        # test with a different geometry unique identifier
        path = self.get_shapefile_path_with_no_ugid()
        geom_select_uid = [12, 15]
        geom_uid = 'ID'
        sc = GeomCabinet()
        records = list(sc.iter_geoms(path=path, uid=geom_uid, select_uid=geom_select_uid))
        self.assertEqual(len(records), 2)
        self.assertEqual([r['properties']['ID'] for r in records], geom_select_uid)
Пример #9
0
    def test_calculation_iteration_two_calculations(self):
        field = self.get_field(with_value=True, month_count=2)
        field.variables.add_variable(Variable(value=field.variables['tmax'].value + 5, name='tmin', alias='tmin'))
        field.temporal.name_uid = 'tid'
        field.level.name_uid = 'lid'
        field.spatial.geom.name_uid = 'gid'

        grouping = ['month']
        tgd = field.temporal.get_grouping(grouping)
        mu = Mean(field=field, tgd=tgd, alias='my_mean', dtype=np.float64, add_parents=True)
        ret = mu.execute()
        thresh = Threshold(field=field, vc=ret, tgd=tgd, alias='a_treshold', add_parents=True,
                           parms={'operation': 'gte', 'threshold': 0.5})
        ret = thresh.execute()

        kwds = copy(field.__dict__)
        kwds.pop('_raw')
        kwds.pop('_variables')
        kwds.pop('_should_regrid')
        kwds.pop('_has_assigned_coordinate_system')
        kwds.pop('_attrs')
        kwds['name'] = kwds.pop('_name')
        kwds['temporal'] = tgd
        kwds['variables'] = ret
        cfield = DerivedField(**kwds)
        cfield.temporal.name_uid = 'tid'
        cfield.temporal.name_value = 'time'
        cfield.spatial.name_uid = 'gid'

        sc = GeomCabinet()
        meta = sc.get_meta('state_boundaries')
        sp = SpatialCollection(meta=meta, key='state_boundaries', headers=constants.HEADERS_CALC)
        for row in sc.iter_geoms('state_boundaries', as_spatial_dimension=True):
            sp.add_field(cfield, ugeom=row)

        cids = set()
        for ii, row in enumerate(sp.get_iter_dict(melted=True)):
            cids.update([row[1]['cid']])
            if ii == 0:
                self.assertEqual(row[0].bounds, (-100.5, 39.5, -99.5, 40.5))
                self.assertDictEqual(row[1], {'lid': 1, 'ugid': 1, 'vid': 1, 'cid': 1, 'did': 1, 'year': 2000,
                                              'time': datetime.datetime(2000, 1, 16, 0, 0),
                                              'calc_alias': 'my_mean_tmax', 'value': 0.44808476666433006, 'month': 1,
                                              'alias': 'tmax', 'variable': 'tmax', 'gid': 1, 'calc_key': 'mean',
                                              'tid': 1, 'level': 50, 'day': 16})
            self.assertEqual(len(row), 2)
            self.assertEqual(len(row[1]), len(constants.HEADERS_CALC))
        self.assertEqual(ii + 1, 2 * 2 * 2 * 3 * 4 * 51 * 4)
        self.assertEqual(len(cids), 4)
Пример #10
0
 def _run_(s, func):
     try:
         ds = ogr.Open(path)
         obj = GeomCabinet._get_features_object_(ds, select_sql_where=s)
         func(obj)
     finally:
         ds.Destroy()
Пример #11
0
    def test_iteration_methods(self):
        field = self.get_field(with_value=True)

        field.temporal.name_uid = 'tid'
        field.level.name_uid = 'lid'
        field.spatial.geom.name_uid = 'gid'
        field.spatial.name_uid = 'gid'

        sc = GeomCabinet()
        meta = sc.get_meta('state_boundaries')
        sp = SpatialCollection(meta=meta, key='state_boundaries', headers=constants.HEADERS_RAW)
        for row in sc.iter_geoms('state_boundaries', as_spatial_dimension=True):
            sp.add_field(field, ugeom=row)
        for ii, row in enumerate(sp.get_iter_dict(melted=True)):
            if ii == 1:
                self.assertDictEqual(row[1], {'lid': 1, 'ugid': 1, 'vid': 1, 'alias': 'tmax', 'did': 1, 'year': 2000,
                                              'value': 0.7203244934421581, 'month': 1, 'variable': 'tmax', 'gid': 2,
                                              'time': datetime.datetime(2000, 1, 1, 12, 0), 'tid': 1, 'level': 50,
                                              'day': 1})
            self.assertIsInstance(row[0], MultiPolygon)
            self.assertEqual(len(row), 2)
            self.assertEqual(len(row[1]), len(constants.HEADERS_RAW))
Пример #12
0
    def test_get_features_object(self):
        # test with a shapefile not having the default unique geometry identifier
        path = self.get_shapefile_path_with_no_ugid()
        keywords = dict(uid=[None, 'ID'],
                        select_uid=[None, [8, 11, 13]],
                        select_sql_where=[None, 'STATE_NAME = "Wisconsin"'])

        for k in self.iter_product_keywords(keywords):
            ds = ogr.Open(path)
            try:
                try:
                    obj = GeomCabinet._get_features_object_(ds, uid=k.uid, select_uid=k.select_uid,
                                                            select_sql_where=k.select_sql_where)
                except RuntimeError:
                    self.assertIsNone(k.uid)
                    self.assertIsNotNone(k.select_uid)
                    continue

                if k.select_sql_where is not None:
                    length = 1
                elif k.select_uid is not None:
                    length = 3
                else:
                    length = 11
                self.assertEqual(len(obj), length)
                self.assertIsInstance(obj, Layer)
            finally:
                ds.Destroy()

        # test on a shapefile having the default unique geometry identifier
        path = GeomCabinet().get_shp_path('state_boundaries')
        ds = ogr.Open(path)
        try:
            obj = GeomCabinet._get_features_object_(ds, select_uid=[8, 11, 13])
            self.assertEqual(len(obj), 3)
        finally:
            ds.Destroy()
Пример #13
0
 def test_iter_geoms_select_ugid(self):
     sc = GeomCabinet()
     it = sc.iter_geoms('state_boundaries', select_uid=[13])
     geoms = list(it)
     self.assertEqual(len(geoms), 1)
     self.assertEqual(geoms[0]['properties']['STATE_NAME'], 'New Hampshire')
Пример #14
0
 def test_iter_geoms_select_sql_where(self):
     sc = GeomCabinet()
     sql = 'STATE_NAME = "New Hampshire"'
     self.assertEqual(len(list(sc.iter_geoms('state_boundaries', select_sql_where=sql))), 1)
Пример #15
0
 def test_iter_geoms_select_ugid_is_sorted(self):
     sc = GeomCabinet()
     with self.assertRaises(ValueError):
         list(sc.iter_geoms('state_boundaries', select_uid=[23, 18]))