0

I wonder how to get size of disk space that a specific vector dataset occupies on a disk. I didn't find any ogr2ogr util or OGR Python method that can return a list of files that belongs to given dataset. I could use ogr2ogr util or CopyDataSource OGR method to copy the dataset to new empty directory using the same driver and analyze the directory then but this method do not preserve original features (and file size) or even does not allow dataset creation for some of OGR formats. Another way is to make a list of possible file extensions that may belongs to given vector format and try to test if the file exists... Any other ideas?

  • There's multiple possible meanings for "size of disk space" -- the actual file size is not the same as the block utilization in disk tracks: it could be more or less. – Vince Dec 29 '15 at 12:39
2

The problem is that shapefiles have only 3 mandatory files and ogr or other Python modules recognize only those (+ eventually the .prj file)

.shp
.dbf
.shx

Many applications as ArcGIS or QGIS add other specific files, not recognized by the modules.

You need to use an artifice as in Retrieving size of shapefile in ArcPy? but the use of a list of extensions as in the script presented is too limited in scope (only for ArcGis).

A proposition, for example

 import glob,os
 shapefiles = [file for file in glob.glob("*.shp")]
 # name of the first shapefile (without extension)
 name = os.path.splitext(shapefiles[0])[0]
 print name
 'test' # test.shp
 # search string
 search = '*'+ name + '*.*'
 for file in glob.glob(search):
     print file, os.path.getsize(file)
 test.dbf 176
 test.prj 497
 test.qpj 783
 test.shp 268
 test.shx 108
 # total size in bytes
 print sum([os.path.getsize(file) for file in glob.glob(search)])
 1832

new

With a filter

extensions = [".shp", ".shx",".dbf"] # without .qpg and .prj
for file in glob.glob(search):
   if os.path.splitext(file)[1] in extensions:
       print file, os.path.getsize(file)
   test.dbf 176
   test.shp 268
   test.shx 108
   print sum([os.path.getsize(file) for file in glob.glob(search) if os.path.splitext(file)[1] in extensions])
   552
  • Thanks for the example. Unfortunately it does not solve case when beside the Shapefile files (test.shp, ...) exist for example another dataset having the same name (e.g. test.gml) - then I get size also of another dataset(s)... – Tonny Dec 29 '15 at 10:33
  • Then you can use a filter with a list of extensions as in Retrieving size of shapefile in ArcPy? – gene Dec 29 '15 at 10:52
  • Yes - it seems it is the only way... – Tonny Dec 29 '15 at 11:02
  • look above in new – gene Dec 29 '15 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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