def open_file(dialog=None, osm_file=None, output_geom_types=None, white_list_column=None, output_format=None, layer_name="OsmFile", config_outputs=None, output_dir=None, prefix_file=None): """ open an osm file """ outputs = get_outputs(output_dir, output_format, prefix_file, layer_name) # Parsing the file osm_parser = OsmParser(osm_file=osm_file, layers=output_geom_types, white_list_column=white_list_column) osm_parser.signalText.connect(dialog.set_progress_text) osm_parser.signalPercentage.connect(dialog.set_progress_percentage) layers = osm_parser.parse() # Finishing the process with geojson or shapefile num_layers = 0 if output_format == "shape": dialog.set_progress_text(tr("QuickOSM", u"From GeoJSON to Shapefile")) if output_format == "spatialite": dialog.set_progress_text(tr("QuickOSM", u"From GeoJSON to SpatiaLite")) # create spatialite DB conn = sqlitedb.connect(outputs['file']) cur = conn.cursor() cur.execute("SELECT initSpatialMetadata(1)") conn.close() for i, (layer, item) in enumerate(layers.iteritems()): dialog.set_progress_percentage(i / len(layers) * 100) QApplication.processEvents() if item['featureCount'] and layer in output_geom_types: final_layer_name = layer_name # If configOutputs is not None (from My Queries) if config_outputs: if config_outputs[layer]['namelayer']: final_layer_name = config_outputs[layer]['namelayer'] # Transforming the vector file osm_geometries = { 'points': QGis.WKBPoint, 'lines': QGis.WKBLineString, 'multilinestrings': QGis.WKBMultiLineString, 'multipolygons': QGis.WKBMultiPolygon } geojson_layer = QgsVectorLayer(item['geojsonFile'], "temp", "ogr") encoding = get_default_encoding() if output_format == "shape": provider = "ESRI Shapefile" elif output_format == "geojson": provider = "GeoJSON" if output_format == "spatialite": uri = QgsDataSourceURI() uri.setDatabase(outputs['file']) uri.setDataSource('', outputs[layer], 'geom') layer_source = uri.uri() layer_provider = 'spatialite' writer = QgsVectorLayerImport(layer_source, layer_provider, geojson_layer.pendingFields(), osm_geometries[layer], geojson_layer.crs()) else: layer_source = outputs[layer] layer_provider = 'ogr' writer = QgsVectorFileWriter(layer_source, encoding, geojson_layer.pendingFields(), osm_geometries[layer], geojson_layer.crs(), provider) for f in geojson_layer.getFeatures(): writer.addFeature(f) del writer # Loading the final vector file new_layer = QgsVectorLayer(layer_source, final_layer_name, layer_provider) # Try to set styling if defined if config_outputs and config_outputs[layer]['style']: new_layer.loadNamedStyle(config_outputs[layer]['style']) else: # Loading default styles if layer == "multilinestrings" or layer == "lines": if "colour" in item['tags']: new_layer.loadNamedStyle( join(dirname(dirname(abspath(__file__))), "styles", layer + "_colour.qml")) # Add action about OpenStreetMap actions = new_layer.actions() actions.addAction( QgsAction.OpenUrl, "OpenStreetMap Browser", 'http://www.openstreetmap.org/browse/' '[% "osm_type" %]/[% "osm_id" %]', False) actions.addAction( QgsAction.GenericPython, 'JOSM', 'from QuickOSM.CoreQuickOSM.Actions import Actions;' 'Actions.run("josm","[% "full_id" %]")', False) actions.addAction( QgsAction.OpenUrl, "User default editor", 'http://www.openstreetmap.org/edit?' '[% "osm_type" %]=[% "osm_id" %]', False) for link in ['url', 'website', 'wikipedia', 'ref:UAI']: if link in item['tags']: link = link.replace(":", "_") actions.addAction( QgsAction.GenericPython, link, 'from QuickOSM.core.actions import Actions;' 'Actions.run("' + link + '","[% "' + link + '" %]")', False) if 'network' in item['tags'] and 'ref' in item['tags']: actions.addAction( QgsAction.GenericPython, "Sketchline", 'from QuickOSM.core.actions import Actions;' 'Actions.run_sketch_line("[% "network" %]","[% "ref" %]")', False) # Add index if possible if output_format == "shape" or output_format == "spatialite": new_layer.dataProvider().createSpatialIndex() QgsMapLayerRegistry.instance().addMapLayer(new_layer) num_layers += 1 return num_layers