QGIS – Fixing Overlapping Layers That Appear Distant

qgis

I have two layers in QGIS (basically 2 maps).. I can see both of them separately when I click on each layer and I press the button "zoom to layer(s)".

In the real world, one of the two maps/layers surrounds completely the second map/layer. However, in QGIS, they look very distant from each other…

Why can't I see them together and overlapping (since they are supposed to), and instead, they look very distant from each other ?

Note 1: One map/layer has these features:


Storage
Memory storage
Encoding

Geometry
Line (LineString)
Extent
401473.9136441830196418,5226800.8761361399665475 : 429895.4095911540207453,5249607.0315812798216939
Feature count
4,945



Coordinate Reference System (CRS)

Name
EPSG:4326 - WGS 84

while the other map/layer has these features:

Storage
ESRI Shapefile
Encoding
UTF-8
Geometry
Polygon (MultiPolygon)
Extent
5.9559019550819725,45.8179584859823805 : 10.4921712689682476,47.8084543074145500
Feature count
4,126



Coordinate Reference System (CRS)

Name
EPSG:4326 - WGS 84

Note 2: The second layer was initially in EPSG:2056 – CH1903+ / LV95, and I then converted it to EPSG:4326 – WGS 84 to "match" the first map/layer.

A few more steps to reproduce my problem:

  1. My small map/layer comes from a list of edges in UTM x- and y- coordinates (WGS84), and I write down here an excerpt of that map for reproducibility (each row is a line/edge in QGIS, x_start,y_start,x_end,y_end):
>> my_qgis_file.csv

ans =

          5244268.06237086          420170.471022784           5244166.4951274          420089.149994671
          5244268.06237086          420170.471022784           5244282.8390319          420075.983838177
          5244268.06237086          420170.471022784          5244203.40710369          420283.463682657
           5239505.2647868          426336.470241881          5239519.18282658          426290.072906479
           5239505.2647868          426336.470241881          5239599.74770874          426390.179540636
  1. I uploaded this small map on QGIS by using this python code in the python console
# specify your csv-file
csvFile = "/Users/.../my_qgis_file.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)
QgsProject.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[1]),float(lineStringAsList[0]))
    to_node = QgsPoint(float(lineStringAsList[3]),float(lineStringAsList[2]))
    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.addFeatures(feats)
layer.updateExtents()
layer.commitChanges()
  1. I downloaded the file PLZO_PLZ.shp, which would represent my large map/layer, from the folder Shape LV95

  2. I tried to reproject PLZO_PLZ.shp from LV95 to WGS84, in order to overlap this large map/layer with the small one. I followed the instructions here, by using "Export –> Save features as…", and by choosing my desired CRS, i.e. WGS84, but still, I have the same issue.

Best Answer

Change your python code so it uses the UTM meter coordinate system of EPSG:32632 UTM zone 32, instead of degrees EPSG:4326

# specify your csv-file 
csvFile = "/Users/.../my_qgis_file.csv" 

# create an empty memory layer for polylines 
layer = QgsVectorLayer('LineString?crs=EPSG:32632', 'Connected', 'memory') # prov = layer.dataProvider()

Note: Your data in my_qgis_file.csv has metric units, so you have to pass to a GIS package the corresponding coordinate system (CRS). Your code ASSIGNS a coordinate system to the data it does not REPROJECT the data. QGIS can work with mixed coordinate systems, there is no need to create a reprojected dataset.

There is a difference between the layer CRS (the CRS of the data) and the QGIS project CRS (the CRS of the view). You are free to choose your project CRS, usually you would use the National standard, so in your case EPSG:2056. UTM is usually used for international projects. EPSG:4326 uses degree as unit and should not be used on a map that is used to depict metric distances and areas.