3

I have a zipped shapefile saved on my disk. I want to extract it in memory and check the shape type. I'm using arcpy module and want to stick to only default modules comes with python installation. I tried something like

import cStringIO, zipfile, arcpy
stringIO = cStringIO.StringIO()
file_ = r"C:\Polyline.zip"
zipFile = zipfile.ZipFile(file_)
for name in zipFile.namelist():
    if name.endswith(".shp"):
        stringIO.writelines(zipFile.read(name))
        print arcpy.Describe(stringIO).shapeType

But it is not working. I got the trace back:

Traceback (most recent call last):
  File "", line 8, in 
  File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy__init__.py", line 1190, in Describe
return gp.describe(value)
File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\geoprocessing_base.py", line 374, in describe
self._gp.Describe(*gp_fixargs(args, True)))
RuntimeError: Object: Describe input value is not valid type

How can I unzip a shapefile in memory and finds its shape type?

8
  • 3
    You are getting hands only at the .shp file from the archive, however for arcpy.Describe to work you need to get hands at other files that compose shapefile (.shx, .shp, .dbf). I would suggest first trying to unzip the file into a temp folder to test the workflow. Commented Feb 28, 2014 at 9:30
  • Thank you for reply, Alex. I tried unzip the files into a temp folder and it works perfectly. But I want to do the same thing in memory without writing down to hard disk. Is there any way to emulate the shapefiles(.shx, *.shp,.dbf,*.prj etc..) inside the memory so that I can check or manipulate it (operations like add records, update or delete records from attribute table)
    – Surya
    Commented Feb 28, 2014 at 11:14
  • There should be some method in Python to unpack multiple files into a memory place, however arcpy should be able to find all the files required in the same workspace and I am frankly speaking don't know if it will be able to refer to the specific portion of memory where the rest of the files were extracted. Let's wait for answers :) Commented Feb 28, 2014 at 11:25
  • 2
    I think you're going to be waiting a long time for this solution. Trying to get at the byte using Python is one thing, but expecting ArcGIS to read files from memory is another.
    – Vince
    Commented Feb 28, 2014 at 13:24
  • 5
    Not adding as an answer since you specified no third-party libraries except arcpy. However, you can do this very simply with the GDAL/OGR python bindings using the "vsizip" virtual filesystem syntax. For example: ds=ogr.Open(r'/vsizip/C:\folder\some_zipfile.zip/some_shapefile.shp') and print ogr.GeometryTypeToName(ds.GetLayer().GetGeomType())
    – user2856
    Commented Mar 5, 2014 at 22:58

1 Answer 1

2

As commented by @Luke:

you can do this very simply with the GDAL/OGR python bindings using the "vsizip" virtual filesystem syntax. For example: ds=ogr.Open(r'/vsizip/C:\folder\some_zipfile.zip/some_shapef‌​ile.shp') and print ogr.GeometryTypeToName(ds.GetLayer().GetGeomType())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.