def test_write_inventory_shapefile(self): inv = read_inventory() with TemporaryWorkingDirectory(): with warnings.catch_warnings(record=True) as w: warnings.filterwarnings('always') _write_shapefile(inv, "inventory.shp") for w_ in w: try: self.assertEqual(str(w_.message), PYSHP_VERSION_WARNING) except AssertionError: continue break else: if not PYSHP_VERSION_AT_LEAST_1_2_11: raise AssertionError('pyshape version warning not shown') for suffix in SHAPEFILE_SUFFIXES: self.assertTrue(os.path.isfile("inventory" + suffix)) with open("inventory.shp", "rb") as fh_shp, \ open("inventory.dbf", "rb") as fh_dbf, \ open("inventory.shx", "rb") as fh_shx: shp = shapefile.Reader(shp=fh_shp, shx=fh_shx, dbf=fh_dbf) # check contents of shapefile that we just wrote _assert_records_and_fields( got_fields=shp.fields, got_records=shp.records(), expected_fields=expected_inventory_fields, expected_records=expected_inventory_records) self.assertEqual(shp.shapeType, shapefile.POINT) _close_shapefile_reader(shp)
def test_write_inventory_shapefile(self): inv = read_inventory() with TemporaryWorkingDirectory(): with warnings.catch_warnings(record=True) as w: warnings.filterwarnings('always') _write_shapefile(inv, "inventory.shp") for w_ in w: try: self.assertEqual( str(w_.message), PYSHP_VERSION_WARNING) except AssertionError: continue break else: if not PYSHP_VERSION_AT_LEAST_1_2_11: raise AssertionError('pyshape version warning not shown') for suffix in SHAPEFILE_SUFFIXES: self.assertTrue(os.path.isfile("inventory" + suffix)) with open("inventory.shp", "rb") as fh_shp, \ open("inventory.dbf", "rb") as fh_dbf, \ open("inventory.shx", "rb") as fh_shx: shp = shapefile.Reader(shp=fh_shp, shx=fh_shx, dbf=fh_dbf) # check contents of shapefile that we just wrote _assert_records_and_fields( got_fields=shp.fields, got_records=shp.records(), expected_fields=expected_inventory_fields, expected_records=expected_inventory_records) self.assertEqual(shp.shapeType, shapefile.POINT) _close_shapefile_reader(shp)
def test_write_inventory_shapefile(self): inv = read_inventory() with TemporaryWorkingDirectory(): _write_shapefile(inv, "inventory.shp") for suffix in SHAPEFILE_SUFFIXES: self.assertTrue(os.path.isfile("inventory" + suffix)) self.assertTrue( filecmp.cmp("inventory" + suffix, self.inventory_shape_basename + suffix), msg="%s not binary equal." % ("inventory" + suffix))
def test_write_catalog_shapefile(self): cat = read_events() with TemporaryWorkingDirectory(): _write_shapefile(cat, "catalog.shp") for suffix in SHAPEFILE_SUFFIXES: self.assertTrue(os.path.isfile("catalog" + suffix)) self.assertTrue( filecmp.cmp("catalog" + suffix, self.catalog_shape_basename + suffix), msg="%s not binary equal." % ("catalog" + suffix))
def test_write_catalog_shapefile(self): cat = read_events() with TemporaryWorkingDirectory(): _write_shapefile(cat, "catalog.shp") for suffix in SHAPEFILE_SUFFIXES: self.assertTrue(os.path.isfile("catalog" + suffix)) self.assertTrue(no_filecmp or filecmp.cmp( "catalog" + suffix, self.catalog_shape_basename + suffix), msg="%s not binary equal." % ("catalog" + suffix))
def test_write_inventory_shapefile(self): inv = read_inventory() with TemporaryWorkingDirectory(): _write_shapefile(inv, "inventory.shp") for suffix in SHAPEFILE_SUFFIXES: self.assertTrue(os.path.isfile("inventory" + suffix)) self.assertTrue( no_filecmp or filecmp.cmp("inventory" + suffix, self.inventory_shape_basename + suffix), msg="%s not binary equal." % ("inventory" + suffix))
def test_write_catalog_shapefile(self): # read two events with uncertainties, one deserializes with "confidence # ellipsoid" origin uncertainty which is not yet implemented for # shapefile output and should show a warning cat = read_events('/path/to/mchedr.dat') cat += read_events('/path/to/nlloc.qml') with TemporaryWorkingDirectory(): with warnings.catch_warnings(record=True) as w: warnings.filterwarnings('always') _write_shapefile(cat, "catalog.shp") for w_ in w: try: self.assertEqual( str(w_.message), 'Encountered an event with origin uncertainty ' 'description of type "confidence ellipsoid". This is ' 'not yet implemented for output as shapefile. No ' 'origin uncertainty will be added to shapefile for ' 'such events.') except AssertionError: continue break else: raise Exception for suffix in SHAPEFILE_SUFFIXES: self.assertTrue(os.path.isfile("catalog" + suffix)) with open("catalog.shp", "rb") as fh_shp, \ open("catalog.dbf", "rb") as fh_dbf, \ open("catalog.shx", "rb") as fh_shx: shp = shapefile.Reader(shp=fh_shp, shx=fh_shx, dbf=fh_dbf) # check contents of shapefile that we just wrote _assert_records_and_fields( got_fields=shp.fields, got_records=shp.records(), expected_fields=expected_catalog_fields, expected_records=expected_catalog_records) self.assertEqual(shp.shapeType, shapefile.POINT) _close_shapefile_reader(shp)
def test_write_catalog_shapefile_with_extra_field(self): """ Tests writing a catalog with an additional custom database column """ cat = read_events('/path/to/mchedr.dat') cat += read_events('/path/to/nlloc.qml') extra_fields = [('Region', 'C', 50, None, ['SOUTHEAST OF HONSHU, JAPAN', 'GERMANY'])] bad_extra_fields_wrong_length = [('Region', 'C', 50, None, ['ABC'])] bad_extra_fields_name_clash = [('Magnitude', 'C', 50, None, ['ABC'])] with TemporaryWorkingDirectory(): with warnings.catch_warnings(record=True) as w: warnings.filterwarnings('always') # test some bad calls that should raise an Exception with self.assertRaises(ValueError) as cm: _write_shapefile( cat, "catalog.shp", extra_fields=bad_extra_fields_wrong_length) self.assertEqual( str(cm.exception), "list of values for each item in " "'extra_fields' must have same length as Catalog object") with self.assertRaises(ValueError) as cm: _write_shapefile( cat, "catalog.shp", extra_fields=bad_extra_fields_name_clash) self.assertEqual( str(cm.exception), "Conflict with existing field named " "'Magnitude'.") # now test a good call that should work _write_shapefile(cat, "catalog.shp", extra_fields=extra_fields) for w_ in w: try: self.assertEqual( str(w_.message), 'Encountered an event with origin uncertainty ' 'description of type "confidence ellipsoid". This is ' 'not yet implemented for output as shapefile. No ' 'origin uncertainty will be added to shapefile for ' 'such events.') except AssertionError: continue break else: raise for suffix in SHAPEFILE_SUFFIXES: self.assertTrue(os.path.isfile("catalog" + suffix)) with open("catalog.shp", "rb") as fh_shp, \ open("catalog.dbf", "rb") as fh_dbf, \ open("catalog.shx", "rb") as fh_shx: shp = shapefile.Reader(shp=fh_shp, shx=fh_shx, dbf=fh_dbf) # check contents of shapefile that we just wrote _assert_records_and_fields( got_fields=shp.fields, got_records=shp.records(), expected_fields=expected_catalog_fields_with_region, expected_records=expected_catalog_records_with_region) self.assertEqual(shp.shapeType, shapefile.POINT) _close_shapefile_reader(shp)
def test_write_inventory_shapefile_with_extra_field(self): """ Tests writing an inventory with an additional custom database column """ inv = read_inventory() extra_fields = [('Comment', 'C', 50, None, ['Abc', None, '123', 'Some comment', ''])] bad_extra_fields_wrong_length = [('Comment', 'C', 50, None, ['ABC'])] bad_extra_fields_name_clash = [('Station', 'C', 50, None, ['ABC'])] with TemporaryWorkingDirectory(): # test some bad calls that should raise an Exception with self.assertRaises(ValueError) as cm: _write_shapefile(inv, "inventory.shp", extra_fields=bad_extra_fields_wrong_length) self.assertEqual( str(cm.exception), "list of values for each item in " "'extra_fields' must have same length as the count of all " "Stations combined across all Networks.") with self.assertRaises(ValueError) as cm: _write_shapefile(inv, "inventory.shp", extra_fields=bad_extra_fields_name_clash) self.assertEqual( str(cm.exception), "Conflict with existing field named " "'Station'.") # now test a good call that should work _write_shapefile(inv, "inventory.shp", extra_fields=extra_fields) for suffix in SHAPEFILE_SUFFIXES: self.assertTrue(os.path.isfile("inventory" + suffix)) with open("inventory.shp", "rb") as fh_shp, \ open("inventory.dbf", "rb") as fh_dbf, \ open("inventory.shx", "rb") as fh_shx: shp = shapefile.Reader(shp=fh_shp, shx=fh_shx, dbf=fh_dbf) # check contents of shapefile that we just wrote _assert_records_and_fields( got_fields=shp.fields, got_records=shp.records(), expected_fields=expected_inventory_fields_with_comment, expected_records=expected_inventory_records_with_comment) self.assertEqual(shp.shapeType, shapefile.POINT) _close_shapefile_reader(shp) # For some reason, on windows the files are still in use when # TemporaryWorkingDirectory tries to remove the directory. self.assertTrue(fh_shp.closed) self.assertTrue(fh_dbf.closed) self.assertTrue(fh_shx.closed)
def test_write_inventory_shapefile_with_extra_field(self): """ Tests writing an inventory with an additional custom database column """ inv = read_inventory() extra_fields = [('Comment', 'C', 50, None, ['Abc', None, '123', 'Some comment', ''])] bad_extra_fields_wrong_length = [('Comment', 'C', 50, None, ['ABC'])] bad_extra_fields_name_clash = [('Station', 'C', 50, None, ['ABC'])] with TemporaryWorkingDirectory(): # test some bad calls that should raise an Exception with self.assertRaises(ValueError) as cm: _write_shapefile( inv, "inventory.shp", extra_fields=bad_extra_fields_wrong_length) self.assertEqual( str(cm.exception), "list of values for each item in " "'extra_fields' must have same length as the count of all " "Stations combined across all Networks.") with self.assertRaises(ValueError) as cm: _write_shapefile( inv, "inventory.shp", extra_fields=bad_extra_fields_name_clash) self.assertEqual( str(cm.exception), "Conflict with existing field named " "'Station'.") # now test a good call that should work _write_shapefile(inv, "inventory.shp", extra_fields=extra_fields) for suffix in SHAPEFILE_SUFFIXES: self.assertTrue(os.path.isfile("inventory" + suffix)) with open("inventory.shp", "rb") as fh_shp, \ open("inventory.dbf", "rb") as fh_dbf, \ open("inventory.shx", "rb") as fh_shx: shp = shapefile.Reader(shp=fh_shp, shx=fh_shx, dbf=fh_dbf) # check contents of shapefile that we just wrote _assert_records_and_fields( got_fields=shp.fields, got_records=shp.records(), expected_fields=expected_inventory_fields_with_comment, expected_records=expected_inventory_records_with_comment) self.assertEqual(shp.shapeType, shapefile.POINT) _close_shapefile_reader(shp) # For some reason, on windows the files are still in use when # TemporaryWorkingDirectory tries to remove the directory. self.assertTrue(fh_shp.closed) self.assertTrue(fh_dbf.closed) self.assertTrue(fh_shx.closed)