Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 读取Azure函数上的形状文件_Python_Azure Functions_Azure Storage Blobs_Shapefile - Fatal编程技术网

Python 读取Azure函数上的形状文件

Python 读取Azure函数上的形状文件,python,azure-functions,azure-storage-blobs,shapefile,Python,Azure Functions,Azure Storage Blobs,Shapefile,我使用python编写的Azure函数来读取来自Blob的形状文件。配置是 { "scriptFile": "__init__.py", "bindings": [ { "name": "inputShp", "type": "blobTrigger", "direction": "in", "path": "uploads/shapefile/{name}.shp", "connection": "StorageCon

我使用python编写的Azure函数来读取来自Blob的形状文件。配置是

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "inputShp",
      "type": "blobTrigger",
      "direction": "in",
      "path": "uploads/shapefile/{name}.shp",
      "connection": "StorageConnectionString"
    }
  ]
}
以及
\uuuu init\uuuu.py
脚本

导入日志
从json导入转储
导入形状文件
将azure.1函数导入为func
def main(输入SHP:函数输入流):
logging.info(f“Python blob触发器函数已处理blob\n”
f“名称:{inputShp.Name}\n”
f“Blob大小:{inputShp.length}字节”)
reader=shapefile.reader(inputShp.read())
但这是一个错误

Exception: ShapefileException: Shapefile Reader requires a shapefile or file-like object.
我尝试使用
reader=shapefile.reader(io.BytesIO(inputShp.read())
,这会导致错误

error: unpack requires a buffer of 4 bytes

您还可以使用关键字参数从任何类似Python文件的对象加载shapefile,以指定这三个文件中的任何一个。此功能非常强大,允许您从url、zip文件、序列化对象或在某些情况下从数据库加载ShapeFile

下面是同样的例子

>>> myshp = open("shapefiles/blockgroups.shp", "rb")
>>> mydbf = open("shapefiles/blockgroups.dbf", "rb")
>>> r = shapefile.Reader(shp=myshp, dbf=mydbf)
使用“打开”而不是“读取”。或者尝试以下示例

from __future__ import print_function
import zipfile
import StringIO
import shapefile

zipshape = zipfile.ZipFile(open(r'C:\GIS\Temp\RoadsShapefileFolder.zip', 'rb'))
print(zipshape.namelist())
dbfname, _, shpname, _, shxname = zipshape.namelist()
r = shapefile.Reader(shp=StringIO.StringIO(zipshape.read(shpname)),
                     shx=StringIO.StringIO(zipshape.read(shxname)),
                     dbf=StringIO.StringIO(zipshape.read(dbfname)))

print(r.bbox)
print(r.numRecords)

这也应该奏效。希望对您有所帮助。

您也可以使用关键字参数从任何类似Python文件的对象加载shapefile,以指定这三个文件中的任何一个。此功能非常强大,允许您从url、zip文件、序列化对象或在某些情况下从数据库加载ShapeFile

下面是同样的例子

>>> myshp = open("shapefiles/blockgroups.shp", "rb")
>>> mydbf = open("shapefiles/blockgroups.dbf", "rb")
>>> r = shapefile.Reader(shp=myshp, dbf=mydbf)
使用“打开”而不是“读取”。或者尝试以下示例

from __future__ import print_function
import zipfile
import StringIO
import shapefile

zipshape = zipfile.ZipFile(open(r'C:\GIS\Temp\RoadsShapefileFolder.zip', 'rb'))
print(zipshape.namelist())
dbfname, _, shpname, _, shxname = zipshape.namelist()
r = shapefile.Reader(shp=StringIO.StringIO(zipshape.read(shpname)),
                     shx=StringIO.StringIO(zipshape.read(shxname)),
                     dbf=StringIO.StringIO(zipshape.read(dbfname)))

print(r.bbox)
print(r.numRecords)

这也应该奏效。希望它有帮助。

< P>从流读取SeaFipe文件似乎是菲奥娜的内存文件(或者MeMyRyZip文件)让你做的。 未测试,但值得检查


< /P> < P>从一个流读取SeaFipe文件似乎是菲奥娜的内存文件(或者MeMyRyZip文件)让你做的。 未测试,但值得检查


文件作为
func.InputStream
传入,因此它们不在任何“物理”位置。只有在从本地位置打开文件时,您的解决方案才有效。了解,这不是一个正确的解决方案,但您可以下载blob并传递路径以供进一步请求。这违背了将Azure和Azure函数中的
blob
一起使用的目的,这不是问题所在。目前我正在服务器上执行此操作,但我希望迁移到Azure函数以使其更好。文件以
func.InputStream
的形式传入,因此它们不在任何“物理”位置。只有在从本地位置打开文件时,您的解决方案才有效。了解,这不是一个正确的解决方案,但您可以下载blob并传递路径以供进一步请求。这违背了将Azure和Azure函数中的
blob
一起使用的目的,这不是问题所在。目前,我正在服务器上执行此操作,但我想迁移到Azure函数以使其更好。嗯,我将在
fiona
上进行一些测试,然后不起作用,显然无法使用
MemoryFile
加载多个文件,对于shapefile,我们需要读取
shp
dbf
shx
文件:啊。你能做的就是在把它放进blob之前用shapefile和所有的sidecar文件创建一个zip文件?然后使用memoryzipfile,但我想shapefile并不是最好的格式。你可以选择使用另一种格式,比如Geoppackage吗?嗯,我会在
fiona
上做一些测试,然后不起作用,显然没有办法用
内存文件加载多个文件,对于shapefile,我们需要读
shp
dbf
shx
文件:啊。你能做的就是在把它放进blob之前用shapefile和所有的sidecar文件创建一个zip文件?然后使用memoryzipfile,但我想shapefile并不是最好的格式。您是否可以选择使用另一种格式,如Geoppackage?