How to draw (or add) a line indicating a specific latitude/longitude?
Here's a Python function that creates a memory layer containing a line at the specified latitude.
You call it using createLatitudeLayer(latitude=-23)
, for example. You can specify which CRS the layer should use by specifying targetCrsEPSG=<EPSG code>
.
You can also specify how many points to use for creating the line, by setting numpoints=<number of points>
. By using more than two points for defining the line, you can also use projections where the lines of equal latitude aren't straight:
import numpy as np
def createLatitudeLayer(latitude=0, numpoints=2, targetCrsEPSG=4326):
sourceCrs = QgsCoordinateReferenceSystem(4326)
targetCrs = QgsCoordinateReferenceSystem(targetCrsEPSG)
transform = QgsCoordinateTransform(sourceCrs, targetCrs).transform
linelayer = QgsVectorLayer("LineString?crs=epsg:" + str(targetCrsEPSG), "latitude of interest", "memory")
line = QgsFeature()
longitudes = np.linspace(-180,180,numpoints)
points = [ transform(QgsPoint(longitude, latitude)) for longitude in longitudes ]
line.setGeometry(QgsGeometry.fromPolyline(points))
linelayer.dataProvider().addFeatures([line])
QgsMapLayerRegistry.instance().addMapLayer(linelayer)
Simply create a text file with this content:
id;wkt
1;LINESTRING(-180 -23, 180 -23)
and use Layer -> Add delimited Text
Menu entry with semicolon as delimiter and EPSG:4326 as CRS.
For meridians, it is better to end the line at 89° when using EPSG:3857:
id;wkt
1;LINESTRING(7 -89, 7 89)
The QuickWKT plugin is very useful for putting temporary lines, points or polygons on the map; a trifle buggy but works.
Install it and you'll get a new toolbar button 'WKT' - click it and you are given a dialogue with space to paste a WKT string, and a drop-down menu to load examples of WKT strings.
I am having trouble with adding a LINESTRING when it has the CRS (SRID=4326;LINESTRING(-180 0, 180 0)
fails with an error), but to show, for example, the equator, enter the plain string:
LINESTRING(-180 0, 180 0)
The format is LINESTRING(LON LAT,LON LAT)
- longitude and latitude separated by a space, then a comma before the next pair of coordinates.
You'll get a dialogue asking for the CRS (4326 in this case) but no matter what you do you'll have to right-click afterwards and re-select the correct CRS. Make sure you have "Enable on-the-fly CRS transformation" selected in your project properties!
To summarise:
- Click the WKT button and paste your WKT text;
- Select the CRS for the WKT you've added;
- Right-click and reselect the CRS.