Esempio n. 1
0
    def test_file_extension(self):
        self.assertRaises(TypeError, util.file_extension, self.ione)
        self.assertRaises(TypeError, util.file_extension, self.fone)
        self.assertRaises(TypeError, util.file_extension, self.btrue)
        self.assertRaises(TypeError, util.file_extension, self.tsimple)
        self.assertRaises(TypeError, util.file_extension, self.lsimple)

	teststring1 = "file.txt"
	self.assertEqual(util.file_extension(teststring1), ".txt")

	teststring2 = "/home/user/file2.ext"
	self.assertEqual(util.file_extension(teststring2), ".ext")
Esempio n. 2
0
def upload_shape_file(request):
    
    theme_id = request.POST['theme']
    theme = models.theme.objects.get(id=int(theme_id))
    file = request.FILES.has_key('image') and request.FILES['image']
    content = request.POST['content']

    if file and file.name:
        filename = file.name
        filepath = None
        idx = 0
        while not filepath or os.path.exists(filepath):
            parts = os.path.splitext(filename)
            filename = parts[0] + str(idx) + parts[1]
            filepath = os.path.join('/tmp/',filename)
            idx += 1
            
        filetype = file_extension(filename.lower())
        is_zip = filetype == 'zip'
        if not is_zip:
            return HttpResponse(generate_error_html("Please upload a ZIP file."));

        try:
            zfile = zipfile.ZipFile(file, 'r')
            corr_extensions = ['dbf', 'prj', 'shp', 'shx']
            extensions = [file_extension(i.lower()) for i in zfile.namelist()]
            good_zip_file = True
            shp_filename = ''
            for ce in corr_extensions:
                if ce not in extensions:
                    good_zip_file = False
                    break
            if not good_zip_file:
                return HttpResponse(generate_error_html("ZIP file should include the following files: .shp, .shx, .dbf, .prj"));
        except:
            return HttpResponse(generate_error_html("Invalid ZIP file."));
        if True:
            for name in zfile.namelist():
                data = zfile.read(name)
                fname = re.sub(r'[^a-zA-Z\d\.]', '', name.lower())
                path = os.path.join('/tmp/', fname)
                if file_extension(fname) == 'shp':
                    shp_filename = fname
                fd = open(path, 'wb')
                fd.write(data)
                fd.close()
                os.chmod(path, 0777)
                
            ########################################
            description = request.POST['content']
            name=request.POST['name'] 
            source=request.POST['source']
            date_start=date_or_null(request.POST['date_start'])
            date_end=date_or_null(request.POST['date_end'])

            ds = DataSource(os.path.join('/tmp/',shp_filename))
            shapes = []
            for layer in ds:
                for feature in layer:
                    fields = {}
                    for fn in feature.fields:
                        if type(fn) == str:
                            fn = "".join(filter(lambda x: ord(x)<128, fn))
                        fields[fn] = feature.get(fn)
                        if type(fields[fn]) == str:
                            fields[fn] = "".join(filter(lambda x: ord(x)<128, fields[fn]))
                    fields_str = simplejson.dumps(fields)
                    trans_geom = feature.geom.transform(4326,clone=True)
                    new_geo_collection = GEOSGeometry(trans_geom.wkt)
                    shapes.append(new_geo_collection)

            new_shape = GeometryCollection(shapes)
            uus = models.UserUploadedShapes(geoms=new_shape,attributes=fields_str,name=name,description=description,theme=theme,source=source,start_date=date_start,end_date=date_end,style='{}')
            uus.save()
            shapes_json = render_to_geojson([uus], models.UserUploadedShapes, 'geoms', raw=True)
            return upload_template(str(shapes_json),'')

    else: 
        return upload_template('{success:False}',generate_error_html("Please upload a valid ZIP file."));
	def _get_format_from_filename(self,filename):
		return file_extension(filename)[1:]
Esempio n. 4
0
 def _get_format_from_filename(self, filename):
     return file_extension(filename)[1:]