def _convert(self, in_crs, in_format, unzip_folder, f_input, f_output): # ogr2ogr -t_srs EPSG:4326 -s_srs EPSG:3857 -f "ESRI Shapefile" query.shp query.geojson error = None if in_format == IMPORT_ESRI: if not os.path.exists(unzip_folder): os.makedirs(unzip_folder) with zipfile.ZipFile(f_input, "r") as z: z.extractall(unzip_folder) os.remove(f_input) f_counter = 0 for filename in [ f for f in os.listdir(unzip_folder) ]: if os.path.splitext(filename)[1] == '.shp': f_counter += 1 f_input = os.path.join(unzip_folder, filename) if f_counter != 1: error = 'invalidContent' if error is None and in_format == IMPORT_ESRI: driver = ogr.GetDriverByName(IMPORT_ESRI) if not driver is None: shapefile = driver.Open(f_input) if not shapefile is None: layer = shapefile.GetLayer() if not layer is None: crs = layer.GetSpatialRef() if not crs is None: try: crs.AutoIdentifyEPSG() except Exception as ex: pass if 'GGRS87' in str(crs): in_crs = 'EPSG:2100' elif 'WGS_84_Pseudo_Mercator' in str(crs): in_crs = 'EPSG:3857' elif 'GCS_WGS_1984' in str(crs): in_crs = 'EPSG:4326' elif 'ETRS89' in str(crs): in_crs = 'EPSG:4258' if error is None and not ogr_export(['', '-lco', 'ENCODING=UTF-8', '-t_srs', 'EPSG:3857', '-s_srs', in_crs, '-f', 'GeoJSON', f_output, f_input]): error = 'conversionFailed' if os.path.exists(unzip_folder): shutil.rmtree(unzip_folder) else: os.remove(f_input) return error
def _export_partial_result(self, text, path, filename, crs, export_format): # ogr2ogr -t_srs EPSG:4326 -s_srs EPSG:3857 -f "ESRI Shapefile" query.shp query.geojson ext = '.' + self._getFormatExtension(export_format) f_input = os.path.join(path, filename + '.geojson') with open(f_input, "w") as text_file: text_file.write(text) if export_format != EXPORT_FORMAT_GEOJSON: f_output = os.path.join(path, filename + ext) if not ogr_export(['', '-lco', 'ENCODING=UTF-8', '-t_srs', 'EPSG:' + str(crs), '-s_srs', 'EPSG:' + str(crs), '-f', export_format, f_output, f_input]): raise DataException('Export operation for CRS [{crs}] and format [{export_format}] has failed'.format(crs = crs, export_format = export_format)) os.remove(f_input)
def _parse_csv(self, in_crs, unzip_folder, f_input, f_output): # ogr2ogr -t_srs EPSG:4326 -s_srs EPSG:3857 -f "GeoJSON" filename.geojson filename.vrt ''' <OGRVRTDataSource> <OGRVRTLayer name="source"> <SrcDataSource>source.csv</SrcDataSource> <GeometryType>wkbPoint</GeometryType> <LayerSRS>WGS84</LayerSRS> <GeometryField encoding="PointFromColumns" x="field_1" y="field_2"/> </OGRVRTLayer> </OGRVRTDataSource> ''' if not os.path.exists(unzip_folder): os.makedirs(unzip_folder) f_input_path, f_input_file = os.path.split(f_input) shutil.move(f_input, os.path.join(unzip_folder, f_input_file)) error = None # Create VRT file vrtFilename = os.path.join(unzip_folder, str(uuid.uuid4()) + '.vrt') with open(vrtFilename, 'w') as f: f.write('<OGRVRTDataSource>') f.write('<OGRVRTLayer name="' + os.path.splitext(f_input_file)[0] + '">') f.write('<SrcDataSource relativeToVRT="1">' + f_input_file + '</SrcDataSource>') f.write('<GeometryType>wkbPoint</GeometryType>') f.write('<LayerSRS>WGS84</LayerSRS>') f.write('<GeometryField encoding="PointFromColumns" x="field_1" y="field_2"/>') f.write('</OGRVRTLayer>') f.write('</OGRVRTDataSource>') if error is None and not ogr_export(['', '-lco', 'ENCODING=UTF-8', '-t_srs', 'EPSG:3857', '-s_srs', in_crs, '-f', 'GeoJSON', f_output, vrtFilename]): error = 'conversionFailed' if os.path.exists(unzip_folder): shutil.rmtree(unzip_folder) return error