コード例 #1
0
ファイル: upload.py プロジェクト: pombredanne/lizard-progress
    def post(self, request, *args, **kwargs):
        if not self.form.is_valid():
            return self.get(request, *args, **kwargs)

        if not self.project.is_manager(request.user):
            return HttpResponseForbidden()

        hydrovakken_dir = directories.hydrovakken_dir(self.project)

        filename = request.FILES['shp'].name

        # Remove old hydrovakken
        for filename in os.listdir(hydrovakken_dir):
            filepath = os.path.join(hydrovakken_dir, filename)
            if os.path.isfile(filepath):
                os.remove(filepath)
        models.Hydrovak.objects.filter(project=self.project).delete()

        # Save uploaded files
        for ext in ['shp', 'dbf', 'shx']:
            uploaded_file = request.FILES[ext]
            with open(
                os.path.join(hydrovakken_dir, uploaded_file.name), 'wb+') as f:
                for chunk in uploaded_file.chunks():
                    f.write(chunk)

        datasource = DataSource(
            os.path.join(hydrovakken_dir, request.FILES['shp'].name))
        id_field_name = configuration.get(
            self.project, 'hydrovakken_id_field')
        layer = datasource[0]

        for feature in layer:
            if id_field_name in feature.fields:
                # The shape can contain both LineStrings and
                # MultiLineStrings - to be able to save both we
                # convert them all to multis
                geom = fromstr(feature.geom.ewkt)
                if isinstance(geom, LineString):
                    geom = MultiLineString(geom)

                hydrovak, created = models.Hydrovak.objects.get_or_create(
                    project=self.project,
                    br_ident=unicode(feature[id_field_name]),
                    defaults={'the_geom': geom})
                if not created:
                    hydrovak.the_geom = geom
                    hydrovak.save()
            else:
                logger.debug("id_field_name not present")
                logger.debug(feature.fields)

        return HttpResponseRedirect(reverse(
                'lizard_progress_downloadhomeview', kwargs={
                    'project_slug': self.project.slug}))
コード例 #2
0
ファイル: views.py プロジェクト: pombredanne/lizard-progress
    def __locations_from_shapefile(self, shapefilepath):
        """Get locations from shapefile and generate them as
        (location_code, WKT string) tuples."""

        shapefile = osgeo.ogr.Open(shapefilepath)

        for layer_num in xrange(shapefile.GetLayerCount()):
            layer = shapefile.GetLayer(layer_num)
            for feature_num in xrange(layer.GetFeatureCount()):
                feature = layer.GetFeature(feature_num)
                location_code = feature.GetField(
                    configuration.get(self.project, 'location_id_field')
                    .encode('utf8'))
                geometry = feature.GetGeometryRef().ExportToWkt()

                yield (location_code, geometry)