def read_vsimem(path, band_number=1): """ Read a single band into memory from a raster. This method does not support all raster formats, only those that are supported by /vsimem Parameters ---------- path: string Path to the raster file. Can be either local or s3/gs. band_number: int The band number to use Returns ------- RasterBand """ filename = str(uuid4()) with fileutils.open(path) as inf: gdal.FileFromMemBuffer("/vsimem/%s" % filename, inf.read()) ds = gdal.Open("/vsimem/%s" % filename) gdal.Unlink("/vsimem/%s" % filename) return RasterBand(ds, band_number=band_number)
def to_json(self, path=None, df=None, precision=6): """Return the layer as a GeoJSON. If a path is provided, it will save to the path. Otherwise, will return a string. Parameters ---------- path: str, (default=None) The path to save the geojson data. df: pandas.DataFrame (default=None) The dataframe to supply the properties of the features. The index of df must match the ids of the VectorLayer. precision: int Number of decimal places to keep for floats Returns ------- geojson string """ res = self.to_dict(df=df) s = pd.io.json.dumps(res, double_precision=precision) if path is None: return s else: with fileutils.open(path, 'wb') as outf: outf.write(s)
def fetch_geojson(path): url = urlparse(path) if "http" in url.scheme: geojson = requests.get(path).text elif "s3" in url.scheme or url.scheme == "" or url.scheme == "file" or url.scheme == "gs": with fileutils.open(path) as inf: geojson = inf.read() else: return path return geojson
def test_raster_io_gs(): filepath = 'data/raster/prism.tif' gs_path = 'gs://%s/%s' % (gs_bucket_name, filepath) # write a raster file to gs outf = fileutils.open(gs_path, "wb") with open(os.path.join(base, filepath)) as inf: outf.write(inf.read()) outf.close() # read the raster file from gs rd = rst.read_raster(gs_path) assert isinstance(rd, rst.RasterDataset) # clean up delete_gs_key(filepath)
def upload(local_filename, remote_path, remove_local=False): """Upload a local file to a remote location. Currently, only s3/gs is suppported""" uri = fileutils.parse_uri(remote_path) if remote_path.endswith("/"): fname = os.path.basename(local_filename) if uri.scheme == "file": uri.uri_path += fname elif uri.scheme in ["s3", "gs"]: uri.key_id += fname else: raise ValueError("%s must be local or s3/gs" % remote_path) outf = fileutils.open(remote_path, "wb") with open(local_filename) as inf: for p in read_in_chunks(inf): outf.write(p) outf.close() if remove_local: os.remove(local_filename) return str(uri)
def upload(local_filename, remote_path, remove_local=False): """Upload a local file to a remote location. Currently, only s3 is suppported""" uri = fileutils.parse_uri(remote_path) if remote_path.endswith("/"): fname = os.path.basename(local_filename) if uri.scheme == "file": uri.uri_path += fname elif uri.scheme == "s3": uri.key_id += fname else: raise ValueError("%s must be local or s3" % remote_path) outf = fileutils.open(remote_path, "wb") with open(local_filename) as inf: for p in read_in_chunks(inf): outf.write(p) outf.close() if remove_local: os.remove(local_filename) return str(uri)
def save(self, path): """Save the html to a file. Can be local or s3. If s3, assumes that a .boto file exists""" with fileutils.open(path, 'wb') as outf: self._render() outf.write(self.html)