def test_dump_load_hex(some_point, tmpdir): file = tmpdir.join("test.wkb") with open(file, "w") as file_pointer: dump(some_point, file_pointer, hex=True) with open(file, "r") as file_pointer: restored = load(file_pointer, hex=True) assert some_point == restored
def test_dump_load_binary(some_point, tmpdir): file = tmpdir.join("test.wkb") with open(file, "wb") as file_pointer: dump(some_point, file_pointer) with open(file, "rb") as file_pointer: restored = load(file_pointer) assert some_point == restored
def test_dump_hex_load_binary(some_point, tmpdir): """Asserts that reading a binary file as text (hex mode) fails.""" file = tmpdir.join("test.wkb") with open(file, "w") as file_pointer: dump(some_point, file_pointer, hex=True) with pytest.raises(WKBReadingError): with open(file, "rb") as file_pointer: load(file_pointer)
def test_dump_binary_load_hex(some_point, tmpdir): """Asserts that reading a text file (hex mode) as binary fails.""" file = tmpdir.join("test.wkb") with open(file, "wb") as file_pointer: dump(some_point, file_pointer) with pytest.raises( (WKBReadingError, UnicodeEncodeError, UnicodeDecodeError)): with open(file, "r") as file_pointer: load(file_pointer, hex=True)
def coast2wkb(lonmin, lonmax, latmin, latmax, GSHHSres, coastfile): # Global cartopy feature from GSHHS gfeat = cfeature.GSHHSFeature(scale=GSHHSres) # As shapely collection generator coll = gfeat.geometries() # Polygon representation of the regional domain frame = geom.box(lonmin, latmin, lonmax, latmax) # The intersection B = (frame.intersection(p) for p in coll if frame.intersects(p)) # Save to file with open(coastfile, mode='wb') as fp: wkb.dump(geom.MultiPolygon(flatten(B)), fp, output_dimension=2)
def test_dump_binary_load_hex(some_point, tmpdir): """Asserts that reading a text file (hex mode) as binary fails.""" file = tmpdir.join("test.wkb") with open(file, "wb") as file_pointer: dump(some_point, file_pointer) # TODO(shapely-2.0) on windows this doesn't seem to error with pygeos, # but you get back a point with garbage coordinates if sys.platform == 'win32': with open(file, "r") as file_pointer: restored = load(file_pointer, hex=True) assert some_point != restored return with pytest.raises((UnicodeEncodeError, UnicodeDecodeError)): with open(file, "r") as file_pointer: load(file_pointer, hex=True)
for ii_w, way in enumerate(result.ways): ls_coords = [] for node in way.nodes: # create a list of node coordinates ls_coords.append((node.lon, node.lat)) # create a LineString from coords lss.append(geometry.LineString(ls_coords)) merged = linemerge([*lss]) # merge LineStrings borders = unary_union(merged) # linestrings to a MultiLineString polygons = list(polygonize(borders)) shapes[area] = geometry.MultiPolygon(polygons) dump(shapes[area], open("{:s}.wkb".format(area), "wb")) # FIXME: What is the canonical location for cached program supplement # data? Should I download it at install-time, when I'm copied somewhere # with write access that might never come back? def contains(shape, coordinates): return shape.contains(geometry.Point(*coordinates)) land_shp_fname = shpreader.natural_earth(resolution='50m', category='physical', name='land') land_geom = unary_union([ record.geometry for record in shpreader.Reader(land_shp_fname).records()