def test_reproject(self): shapefile.copy_shp(self.shapefile, self.outfile, reproject=4326) with fiona.open(self.outfile) as collection: self.assertAlmostEqual(collection[0]['geometry']['coordinates'][0], -119.99999, 4) self.assertAlmostEqual(collection[0]['geometry']['coordinates'][1], 38.01636, 4)
def test_append(self): out = os.path.join(TEST_RES_DIR, 'appended.shp') shapefile.copy_shp(self.shapefile, out) shapefile.copy_shp(self.shapefile, out, append=True) with fiona.open(out) as collection: self.assertEqual(len(collection), 10) self.assertEqual(len(collection[1]['properties']), 8)
def test_filtering(self): def filtr(row): return row['number'] == 1 shapefile.copy_shp(self.shapefile, self.outfile, filter_function=filtr) with fiona.open(self.outfile) as collection: self.assertEqual(len(collection), 1) for item in collection: self.assertIn(item['properties']['number'], [1, 3])
def test_reproject_with_remap(self): def remap(row): return row shapefile.copy_shp(self.shapefile, self.outfile, reproject=4326, remap_function=remap) with fiona.open(self.shapefile) as collection: self.assertEqual(collection.crs, {'init': 'epsg:3310'})
def test_subset_cols(self): fields = ['one', 'THREE', 'four'] shapefile.copy_shp(self.shapefile, self.outfile, fields=fields) with fiona.open(self.outfile) as collection: self.assertEqual(len(collection), 5) self.assertEqual(len(collection[1]['properties']), 3) for field in fields: self.assertIn(field, collection[2]['properties']) for field in ['two', 'five', 'six', 'number']: self.assertNotIn(field, collection[3]['properties'])
def test_simple_copy(self): shapefile.copy_shp(self.shapefile, self.outfile) with fiona.open(self.outfile) as collection: self.assertEqual(len(collection), 5) self.assertEqual(len(collection[1]['properties']), 8) # make sure it overwrites shapefile.copy_shp(self.shapefile, self.outfile) with fiona.open(self.outfile) as collection: self.assertEqual(len(collection), 5) self.assertEqual(len(collection[1]['properties']), 8)
def test_sort(self): with fiona.open(self.shapefile) as collection: values = [item['properties']['one'] for item in collection] unsorted = values.copy() values.sort() self.assertNotEqual(unsorted, values) shapefile.copy_shp(self.shapefile, self.outfile, sort=['one']) with fiona.open(self.outfile) as collection: values = [item['properties']['one'] for item in collection] unsorted = values.copy() values.sort() self.assertEqual(unsorted, values)
def test_remap_function(self): def remap(row): row['zwei'] = row['two'] del row['two'] return row shapefile.copy_shp(self.shapefile, self.outfile, remap_function=remap) with fiona.open(self.outfile) as collection: self.assertEqual(len(collection), 5) fields = ['one', 'zwei', 'THREE', 'four', 'five', 'six', 'number'] for field in fields: self.assertIn(field, collection[4]['properties']) self.assertNotIn('two', collection[0]['properties'])
def test_crs(self): shapefile.copy_shp(self.shapefile, self.outfile) with fiona.open(self.outfile) as collection: self.assertEqual(collection.crs, {'init': 'epsg:3310'})
def test_remap(self): shapefile.copy_shp(self.shapefile, self.outfile, remap={'two': 'zwei'}) with fiona.open(self.outfile) as collection: for item in collection: print(item)