1. Dane Springmeyer
  2. django-shapes

Commits

spri...@gmail.com  committed 03d8926

Fixed missing import and adding checking for remainder of standard shapefile extensions

  • Participants
  • Parent commits 4f272a9
  • Branches default

Comments (0)

Files changed (2)

File shapes/forms.py

View file
           destination.write(chunk)
       destination.close()        
 
+    def zip_check(self, ext, zip_file):
+      if not True in [info.filename.endswith('shp') for info in zipfile.infolist()]:
+        return False
+      return True
+      
     def validate(self,uploaded_file):
       tmp = tempfile.NamedTemporaryFile(suffix='.shp', mode = 'w')
       tmp_dir = tempfile.gettempdir()
           destination.write(chunk)
       destination.close()
       if not zipfile.is_zipfile(tmp.name):
-        return False, 'That file is not a Zip Archive'
+        return False, 'That file is not a valid Zip Archive'
       else:
         zfile = zipfile.ZipFile(tmp.name)
-        if not True in [info.filename.endswith('shp') for info in zfile.infolist()]:
-          return False, 'Found Zip Archive but no file with a .shp extension found inside'
+        if not self.zip_check('shp', zfile):
+          return False, 'Found Zip Archive but no file with a .shp extension found inside.'
+        elif not self.zip_check('prj', zfile):
+          return False, 'You must supply a .prj file with the Shapefile to indicate the projection.'
+        elif not self.zip_check('dbf', zfile):
+          return False, 'You must supply a .dbf file with the Shapefile to supply attribute data.'
+        elif not self.zip_check('shx', zfile):
+          return False, 'You must supply a .shx file for the Shapefile to have a valid index.'
         else:
-          if not True in [info.filename.endswith('prj') for info in zfile.infolist()]:
-            return False, 'You must supply a .prj file with the Shapefile to indicate the projection'
+          for info in zfile.infolist():
+            data = zfile.read(info.filename)
+            shp_part = '%s%s' % (tmp_dir, info.filename)
+            fout = open(shp_part, "wb")
+            fout.write(data)
+            fout.close()
+            # http://code.djangoproject.com/wiki/GeoDjangoExtras#DataSource
+          ds_name = zfile.infolist()[0].filename.split('.')[0]
+          ds = DataSource('%s%s.shp' % (tmp_dir, ds_name))
+          layer = ds[0]
+          if layer.test_capability('RandomRead'):
+            if ds._driver.__str__() == 'ESRI Shapefile':
+              return True, None
+            else:
+              return False, "Sorry, we've experienced a problem on our server. Please try again later."
           else:
-            for info in zfile.infolist():
-              data = zfile.read(info.filename)
-              shp_part = '%s%s' % (tmp_dir, info.filename)
-              fout = open(shp_part, "wb")
-              fout.write(data)
-              fout.close()
-              # http://code.djangoproject.com/wiki/GeoDjangoExtras#DataSource
-            ds_name = zfile.infolist()[0].filename.split('.')[0]
-            ds = DataSource('%s%s.shp' % (tmp_dir, ds_name))
-            layer = ds[0]
-            if layer.test_capability('RandomRead'):
-              if ds._driver.__str__() == 'ESRI Shapefile':
-                return True, None
-              else:
-                return False, "Sorry, we've experienced a problem on our server. Please try again later."
-            else:
-                return False, 'Cannot read the shapefile, data is corrupted inside the zip, please try to upload again' 
+              return False, 'Cannot read the shapefile, data is corrupted inside the zip, please try to upload again' 

File shapes/views/export.py

View file
 from django.contrib.gis.gdal.libgdal import lgdal
 
 #from django.contrib.gis.gdal import *
-from django.contrib.gis.gdal import OGRGeomType, SpatialReference, check_err
+from django.contrib.gis.gdal import OGRGeometry, OGRGeomType, SpatialReference, check_err
 
 # todo use: qs.query._geo_field()
 # todo abstract lgdal stuff