Esempio n. 1
0
    def write_files(self):

        absolute_base_file = None
        tempdir = tempfile.mkdtemp()

        if zipfile.is_zipfile(self.cleaned_data['base_file']):
            absolute_base_file = unzip_file(self.cleaned_data['base_file'], '.shp', tempdir=tempdir)

        else:
            for field in self.spatial_files:
                f = self.cleaned_data[field]
                if f is not None:
                    path = os.path.join(tempdir, f.name)
                    with open(path, 'wb') as writable:
                        for c in f.chunks():
                            writable.write(c)
            absolute_base_file = os.path.join(tempdir,
                                              self.cleaned_data["base_file"].name)
        return tempdir, absolute_base_file
Esempio n. 2
0
    def write_files(self):

        absolute_base_file = None
        tempdir = tempfile.mkdtemp()

        if zipfile.is_zipfile(self.cleaned_data['base_file']):
            absolute_base_file = unzip_file(self.cleaned_data['base_file'], '.shp', tempdir=tempdir)

        else:
            for field in self.spatial_files:
                f = self.cleaned_data[field]
                if f is not None:
                    path = os.path.join(tempdir, f.name)
                    with open(path, 'wb') as writable:
                        for c in f.chunks():
                            writable.write(c)
            absolute_base_file = os.path.join(tempdir,
                                              self.cleaned_data["base_file"].name)
        return tempdir, absolute_base_file
Esempio n. 3
0
    def write_files(self):

        absolute_base_file = None
        tempdir = tempfile.mkdtemp()
        file = self.cleaned_data['base_file']
        filename = file.name
        extension = os.path.splitext(filename)[1]
        file_type = None
        if extension.lower() == '.osm':
            file_type = ".osm"
            osm_layer_type = self.cleaned_data['osm_layer_type']
            tempdir_osm = tempfile.mkdtemp(
            )  # temporary directory for uploading .osm file
            temporary_file = open('%s/%s' % (tempdir_osm, filename), 'a+')

            for chnk in file.chunks():
                temporary_file.write(chnk)
            temporary_file.close()
            file_path = temporary_file.name
            from geonode.layers.utils import ogrinfo
            response = ogrinfo(file_path)
            if osm_layer_type in response:

                from plumbum.cmd import ogr2ogr
                ogr2ogr[tempdir, file_path, osm_layer_type]()
                files = os.listdir(tempdir)
                for item in files:
                    if item.endswith('.shp'):
                        absolute_base_file = os.path.join(tempdir, item)
            else:
                raise forms.ValidationError(
                    'You are trying to upload {0} layer but the .osm file you provided contains'
                    '{1}'.format(osm_layer_type, response))

        elif extension.lower() == '.csv':
            file_type = ".csv"
            the_geom = self.cleaned_data['the_geom']
            longitude = self.cleaned_data['longitude']  #longitude
            lattitude = self.cleaned_data['lattitude']  #latitude
            tempdir_csv = tempfile.mkdtemp(
            )  # temporary directory for uploading .csv file
            temporary_file = open('%s/%s' % (tempdir_csv, filename), 'a+')
            for chnk in file.chunks():
                temporary_file.write(chnk)
            temporary_file.close()
            file_path = temporary_file.name

            temp_vrt_path = os.path.join(tempdir_csv, "tempvrt.vrt")
            temp_vrt_file = open(temp_vrt_path, 'wt')
            vrt_string = """  <OGRVRTDataSource>
                    <OGRVRTLayer name="222ogr_vrt_layer_name222">
                    <SrcDataSource>000path_to_the_imported_csv_file000</SrcDataSource>
                    <GeometryType>wkbUnknown</GeometryType>
                    <GeometryField encoding="WKT" field="111the_field_name_geom_for_csv111"/>
                    </OGRVRTLayer>
                    </OGRVRTDataSource> """

            vrt_string_point = """
                <OGRVRTDataSource>
                    <OGRVRTLayer name="222ogr_vrt_layer_name222">
                        <SrcDataSource>000path_to_the_imported_csv_file000</SrcDataSource>
			            <SrcLayer>222ogr_vrt_layer_name222</SrcLayer>
                        <GeometryType>wkbPoint</GeometryType>
		                <LayerSRS>WGS84</LayerSRS>
                        <GeometryField encoding="PointFromColumns" x="333y_for_longitude_values333" y="111x_for_lattitude_values111"/>
                    </OGRVRTLayer>
                </OGRVRTDataSource>"""

            layer_name, ext = os.path.splitext(filename)

            if longitude and lattitude:
                vrt_string_point = vrt_string_point.replace(
                    "222ogr_vrt_layer_name222", layer_name)
                vrt_string_point = vrt_string_point.replace(
                    "000path_to_the_imported_csv_file000", file_path)
                vrt_string_point = vrt_string_point.replace(
                    "111x_for_lattitude_values111", lattitude)
                vrt_string_point = vrt_string_point.replace(
                    "333y_for_longitude_values333", longitude)
                temp_vrt_file.write(vrt_string_point)
            elif the_geom:
                vrt_string = vrt_string.replace("222ogr_vrt_layer_name222",
                                                layer_name)
                vrt_string = vrt_string.replace(
                    "000path_to_the_imported_csv_file000", file_path)
                vrt_string = vrt_string.replace(
                    "111the_field_name_geom_for_csv111", the_geom)
                temp_vrt_file.write(vrt_string)

            temp_vrt_file.close()
            ogr2ogr_string = 'ogr2ogr -overwrite -f "ESRI Shapefile" '
            ogr2ogr_string = ogr2ogr_string + '"' + tempdir + '"' + ' ' + '"' + temp_vrt_path + '"'

            os.system(ogr2ogr_string)

            files = os.listdir(tempdir)
            for item in files:
                if item.endswith('.shp'):
                    shape_file = shapefile.Reader(os.path.join(tempdir, item))
                    shapes = shape_file.shapes()
                    names = [
                        name for name in dir(shapes[1])
                        if not name.startswith('__')
                    ]
                    if not 'bbox' in names and the_geom:
                        raise forms.ValidationError(
                            'The "geom" field of your .csv file does not contains valid multistring points '
                            'or your uploaded file does not contains valid layer'
                        )
                    else:
                        absolute_base_file = os.path.join(tempdir, item)

        elif zipfile.is_zipfile(self.cleaned_data['base_file']):
            file_type = ".zip"
            absolute_base_file = unzip_file(self.cleaned_data['base_file'],
                                            '.shp',
                                            tempdir=tempdir)

        else:
            file_type = "shape files"
            for field in self.spatial_files:
                f = self.cleaned_data[field]
                if f is not None:
                    path = os.path.join(tempdir, f.name)
                    with open(path, 'wb') as writable:
                        for c in f.chunks():
                            writable.write(c)
            absolute_base_file = os.path.join(
                tempdir, self.cleaned_data["base_file"].name)
        return tempdir, absolute_base_file, file_type
Esempio n. 4
0
    def write_files(self):

        absolute_base_file = None
        tempdir = tempfile.mkdtemp()
        file = self.cleaned_data['base_file']
        filename = file.name
        extension = os.path.splitext(filename)[1]
        if extension.lower() == '.osm':
            osm_layer_type = self.cleaned_data['osm_layer_type']
            tempdir_osm = tempfile.mkdtemp()  # temporary directory for uploading .osm file
            temporary_file = open('%s/%s' % (tempdir_osm, filename), 'a+')

            for chnk in file.chunks():
                temporary_file.write(chnk)
            temporary_file.close()
            file_path = temporary_file.name
            from geonode.layers.utils import ogrinfo
            response = ogrinfo(file_path)
            if osm_layer_type in response:

                from plumbum.cmd import ogr2ogr
                ogr2ogr[tempdir, file_path, osm_layer_type]()
                files = os.listdir(tempdir)
                for item in files:
                    if item.endswith('.shp'):
                        absolute_base_file = os.path.join(tempdir, item)
            else:
                raise forms.ValidationError('You are trying to upload {0} layer but the .osm file you provided contains'
                                            '{1}'.format(osm_layer_type, response))

        elif extension.lower() == '.csv':
            the_geom = self.cleaned_data['the_geom']
            longitude = self.cleaned_data['longitude'] #longitude
            lattitude = self.cleaned_data['lattitude'] #latitude
            tempdir_csv = tempfile.mkdtemp()  # temporary directory for uploading .csv file
            temporary_file = open('%s/%s' % (tempdir_csv, filename), 'a+')
            for chnk in file.chunks():
                temporary_file.write(chnk)
            temporary_file.close()
            file_path = temporary_file.name

            temp_vrt_path = os.path.join(tempdir_csv, "tempvrt.vrt")
            temp_vrt_file = open(temp_vrt_path, 'wt')
            vrt_string = """  <OGRVRTDataSource>
                    <OGRVRTLayer name="222ogr_vrt_layer_name222">
                    <SrcDataSource>000path_to_the_imported_csv_file000</SrcDataSource>
                    <GeometryType>wkbUnknown</GeometryType>
                    <GeometryField encoding="WKT" field="111the_field_name_geom_for_csv111"/>
                    </OGRVRTLayer>
                    </OGRVRTDataSource> """

            vrt_string_point = """
                <OGRVRTDataSource>
                    <OGRVRTLayer name="222ogr_vrt_layer_name222">
                        <SrcDataSource>000path_to_the_imported_csv_file000</SrcDataSource>
			            <SrcLayer>222ogr_vrt_layer_name222</SrcLayer>
                        <GeometryType>wkbPoint</GeometryType>
		                <LayerSRS>WGS84</LayerSRS>
                        <GeometryField encoding="PointFromColumns" x="333y_for_longitude_values333" y="111x_for_lattitude_values111"/>
                    </OGRVRTLayer>
                </OGRVRTDataSource>"""

            layer_name, ext = os.path.splitext(filename)

            if longitude and lattitude:
                vrt_string_point = vrt_string_point.replace("222ogr_vrt_layer_name222", layer_name)
                vrt_string_point = vrt_string_point.replace("000path_to_the_imported_csv_file000", file_path)
                vrt_string_point = vrt_string_point.replace("111x_for_lattitude_values111", lattitude)
                vrt_string_point = vrt_string_point.replace("333y_for_longitude_values333", longitude)
                temp_vrt_file.write(vrt_string_point)
            elif the_geom:
                vrt_string = vrt_string.replace("222ogr_vrt_layer_name222", layer_name)
                vrt_string = vrt_string.replace("000path_to_the_imported_csv_file000", file_path)
                vrt_string = vrt_string.replace("111the_field_name_geom_for_csv111", the_geom)
                temp_vrt_file.write(vrt_string)

            temp_vrt_file.close()
            ogr2ogr_string = 'ogr2ogr -overwrite -f "ESRI Shapefile" '
            ogr2ogr_string = ogr2ogr_string+'"'+tempdir+'"'+' '+'"'+temp_vrt_path+'"'

            os.system(ogr2ogr_string)

            files = os.listdir(tempdir)
            for item in files:
                if item.endswith('.shp'):
                    shape_file = shapefile.Reader(os.path.join(tempdir, item))
                    shapes = shape_file.shapes()
                    names = [name for name in dir(shapes[1]) if not name.startswith('__')]
                    if not 'bbox' in names and the_geom:
                        raise forms.ValidationError('The "geom" field of your .csv file does not contains valid multistring points '
                                                    'or your uploaded file does not contains valid layer')
                    else:
                        absolute_base_file = os.path.join(tempdir, item)


        elif zipfile.is_zipfile(self.cleaned_data['base_file']):
            absolute_base_file = unzip_file(self.cleaned_data['base_file'], '.shp', tempdir=tempdir)

        else:
            for field in self.spatial_files:
                f = self.cleaned_data[field]
                if f is not None:
                    path = os.path.join(tempdir, f.name)
                    with open(path, 'wb') as writable:
                        for c in f.chunks():
                            writable.write(c)
            absolute_base_file = os.path.join(tempdir,
                                              self.cleaned_data["base_file"].name)
        return tempdir, absolute_base_file