6

I'm trying to add a single line that show a specific latitude (e.g. the Tropic of Cancer) I don't want a gird but only a single line even in a vector layer.How can I do that I've try with pyQGIS console but seems I can't...

I would like to measure angle with that line so I'd like to have it in a layer. However I found useful a method to use it in print composer, but my main goal is to have it as line in QGIS.

For the moment I'm using Google Satellite open layers with a EPSG:3857.

1
  • Is this just for display purposes (i.e. could it be done in the print composer), or do you want to use that line in some kind of analysis (i.e. clip other layers with it, for example)? What kind of projection are you using?
    – Jake
    Nov 26, 2013 at 10:52

7 Answers 7

9

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)
1
  • +1 Thanks works, I don't understand why when I move the mouse over I've got -26 instead of -23...
    – G M
    Nov 26, 2013 at 13:35
7

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)
3
  • @GM: Ah, I see. No problem, I've edited the answer.
    – Jake
    Nov 26, 2013 at 14:06
  • Sorry again! usually I wait a couple of days before accepting an answer, but your is very good maybe I'll do it before. Thanks a lot for the help!
    – G M
    Nov 26, 2013 at 14:12
  • @GM: Waiting before accepting answers is a good idea, maybe someone will come with something amazing. No rush from my side =)
    – Jake
    Nov 26, 2013 at 14:14
7

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!

enter image description here

To summarise:

  1. Click the WKT button and paste your WKT text;
  2. Select the CRS for the WKT you've added;
  3. Right-click and reselect the CRS.
2
  • +1 It works so to summarize I specify the parameters on LINESTRING(long. lat., long. lat.) click OK than I have to go on properties of the new layer and specify the CRS 4326 again. Thanks for the help!
    – G M
    Nov 26, 2013 at 14:27
  • Have updated the answer a bit – should have explained more, thanks for the hint.
    – Simbamangu
    Nov 27, 2013 at 12:54
1

Inspired by Jakes Answer I tried rewriting the function with QGIS version 3.26.3 as the API has had some breaking changes since written.

import numpy as np

def createLatitudeLayer(latitude=66, numpoints=10, targetCrsEPSG="EPSG:4326"):
    sourceCrs = QgsCoordinateReferenceSystem("EPSG:4326")
    targetCrs = QgsCoordinateReferenceSystem(targetCrsEPSG)
    transform = QgsCoordinateTransform(sourceCrs, targetCrs, QgsProject.instance())
    linelayer = QgsVectorLayer("LineString?crs=epsg:" + str(targetCrs), "latitude of interest", "memory")
    line = QgsFeature()
    longitudes = np.linspace(-180,180,numpoints)

    points = [ QgsPoint(longitude, latitude) for longitude in longitudes ]

    line.setGeometry(QgsGeometry.fromPolyline(points))
    linelayer.dataProvider().addFeatures([line])
    QgsProject.instance().addMapLayer(linelayer)

createLatitudeLayer(latitude=0)
1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Oct 18, 2022 at 16:56
0

Select Create a Line using create features interface. Then, right click for the first point of the line and select absolute X Y and put in your X and Y Values (lat and long). Repeat for second point of line. Alternatively, create a line and then right click and select "Move to" and change the x/y values. (Note that the drop down box within of the absolute x y window will allow you to specify lat long in DMS or DM, etc.).

0

For a full grid of 10 degrees latitude by 10 degrees longitude, add the following to a file 'latlon.txt':

id,wkt
1,"SRID=4326;LINESTRING(-180 80, 180 80)"
2,"SRID=4326;LINESTRING(-180 70, 180 70)"
3,"SRID=4326;LINESTRING(-180 60, 180 60)"
4,"SRID=4326;LINESTRING(-180 50, 180 50)"
5,"SRID=4326;LINESTRING(-180 40, 180 40)"
6,"SRID=4326;LINESTRING(-180 30, 180 30)"
7,"SRID=4326;LINESTRING(-180 20, 180 20)"
8,"SRID=4326;LINESTRING(-180 10, 180 10)"
9,"SRID=4326;LINESTRING(-180 0, 180 0)"
10,"SRID=4326;LINESTRING(-180 -10, 180 -10)"
11,"SRID=4326;LINESTRING(-180 -20, 180 -20)"
12,"SRID=4326;LINESTRING(-180 -30, 180 -30)"
13,"SRID=4326;LINESTRING(-180 -40, 180 -40)"
14,"SRID=4326;LINESTRING(-180 -50, 180 -50)"
15,"SRID=4326;LINESTRING(-180 -60, 180 -60)"
16,"SRID=4326;LINESTRING(-180 -70, 180 -70)"
17,"SRID=4326;LINESTRING(-180 -80, 180 -80)"
18,"SRID=4326;LINESTRING(-180 85, -180 -85)"
19,"SRID=4326;LINESTRING(-170 85, -170 -85)"
20,"SRID=4326;LINESTRING(-160 85, -160 -85)"
21,"SRID=4326;LINESTRING(-150 85, -150 -85)"
22,"SRID=4326;LINESTRING(-140 85, -140 -85)"
23,"SRID=4326;LINESTRING(-130 85, -130 -85)"
24,"SRID=4326;LINESTRING(-120 85, -120 -85)"
25,"SRID=4326;LINESTRING(-110 85, -110 -85)"
26,"SRID=4326;LINESTRING(-100 85, -100 -85)"
27,"SRID=4326;LINESTRING(-90 85, -90 -85)"
28,"SRID=4326;LINESTRING(-80 85, -80 -85)"
29,"SRID=4326;LINESTRING(-70 85, -70 -85)"
30,"SRID=4326;LINESTRING(-60 85, -60 -85)"
31,"SRID=4326;LINESTRING(-50 85, -50 -85)"
32,"SRID=4326;LINESTRING(-40 85, -40 -85)"
33,"SRID=4326;LINESTRING(-30 85, -30 -85)"
34,"SRID=4326;LINESTRING(-20 85, -20 -85)"
35,"SRID=4326;LINESTRING(-10 85, -10 -85)"
36,"SRID=4326;LINESTRING(0 85, 0 -85)"
37,"SRID=4326;LINESTRING(10 85, 10 -85)"
38,"SRID=4326;LINESTRING(20 85, 20 -85)"
38,"SRID=4326;LINESTRING(30 85, 30 -85)"
38,"SRID=4326;LINESTRING(40 85, 40 -85)"
38,"SRID=4326;LINESTRING(50 85, 50 -85)"
38,"SRID=4326;LINESTRING(60 85, 60 -85)"
38,"SRID=4326;LINESTRING(70 85, 70 -85)"
38,"SRID=4326;LINESTRING(80 85, 80 -85)"
38,"SRID=4326;LINESTRING(90 85, 90 -85)"
38,"SRID=4326;LINESTRING(100 85, 100 -85)"
38,"SRID=4326;LINESTRING(110 85, 110 -85)"
38,"SRID=4326;LINESTRING(120 85, 120 -85)"
38,"SRID=4326;LINESTRING(130 85, 130 -85)"
38,"SRID=4326;LINESTRING(140 85, 140 -85)"
38,"SRID=4326;LINESTRING(150 85, 150 -85)"
38,"SRID=4326;LINESTRING(160 85, 160 -85)"
38,"SRID=4326;LINESTRING(170 85, 170 -85)"

Then import this file through menu: Layer=>Add Layer=>Add delimited text layer...

Select the file and check the following settings: comma as delimiter, " as quote, 'WKT' for Geometry defintion and 'EPSG:4326 - WGS 84' as Geometry CRS

Click the 'Add' button

0

You can use a Virtual Layer:

select makeline(makepoint(-180, 23.43633, 4326), makepoint(180,23.43633,4326)) as geom

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.