I have a CSV edge list such as follows:
Lat1,Lon1,Lat2,Lon2 93.41823,-104.324324,93.41828,-104.324320 93.40110,-104.310552,93.40108,-104.310557 ...
Is there a way to import such a CSV file into QGis such that the edges show up as lines?
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.Sign up to join this community
Replace test.csv with the correct filename and paste the following code into the Python console. Note, that in your csv Lat=y comes before Lon=x.
# specify your csv-file csvFile = "G:/test.csv" # create an empty memory layer for polylines layer = QgsVectorLayer('LineString?crs=EPSG:4326', 'Connected', 'memory') prov = layer.dataProvider() # add layer to the map QgsMapLayerRegistry.instance().addMapLayer(layer) # open the csv-file for reading and skip the header row lineStrings = open(csvFile, "rU") next(lineStrings) # start editing layer.startEditing() # loop over the lines, split them into 4 coordinates, build points from pairs of # them, and connect the pair of points feats =  for line in lineStrings: lineStringAsList = line.split(",") from_node = QgsPoint(float(lineStringAsList),float(lineStringAsList)) to_node = QgsPoint(float(lineStringAsList),float(lineStringAsList)) feat = QgsFeature() feat.setGeometry(QgsGeometry.fromPolyline([from_node, to_node])) feats.append(feat) # finally add all created features and save edits prov.addFeatures(feats) layer.updateExtents() layer.commitChanges()
Please check your latitude values, they are out of range [-90, 90].