4

I'm looking for an easier way to calculate the surface on QGIS:

My project CRS is in Lambert93.

Currently, when I'm calculating a surface using QGIS, when the surface seems wrong, I always have to:

  • "Save as..." my current layer under a new CRS (Lambert93 in my case, (see Capture 1).
  • Then the new layer appears, and the $area calculation seems correct. (Capture 2)

My question is: this process doesn't seems very efficient as it creates new files on my computer; is there any other way to do this? (ie. without Save as... new layer, neither creating new files, ...)


Capture 1 : Save as the layer under a new CRS (Lambert93) enter image description here

Capture 2 : my $area is now correct ! enter image description here

  • It looks like a problem with transform layers. Please check Tools>Option>CRS, put your default CRS and select reprojection on the fly. – Artec Jan 10 '16 at 11:22
  • @Artec AFAIK the area calculation is made in the CRS of the feature, not after the 'on the fly reprojection'. – Zoltan Jan 10 '16 at 15:17
  • @Artec: I tried this, I put everything to LAMBERT93 --- but even when I right click on my layer 'Surface2004' -> "Define the CRS of this layer" --> "Lambert 93", when I'm calculating the surface, it's still returning an weird result (whereas it's going well when I'm using the "Save as..." function) -- Any ideas? Thanks! – Hajeong Jan 10 '16 at 15:40
  • 1
    Can you share a sample data ? – Artec Jan 10 '16 at 18:53
  • 1
    @Hajeong - Using "Define the CRS of this layer" does not change reproject the layer hence why you do not receive the correct results. The shapefile must be exported using the "Save As..." option which does reproject the data according to the new CRS definition. – Joseph Jan 11 '16 at 13:23
1

You could create a memory layer of the desired layer using Python via the Python Console which you can access from the toolbar (Plugins > Python Console). This method will:

  • Allow you to define a CRS
  • Copy all features and attributes from desired layer
  • Will not be physically saved

Select a layer and use the following code in the Python Console:

input = qgis.utils.iface.activeLayer()
layer = QgsVectorLayer(input.source(),"polygon","ogr")
feats = [ feat for feat in layer.getFeatures() ]
temp = QgsVectorLayer("Polygon?crs=epsg:102110", "name", "memory")
# 'temp' is the new memory layer
# Change 'LineString' to 'Point' or 'Polygon' etc depending on your layer type
# # Change CRS to desired CRS using the EPSG code; change "name" to whatever name you want

temp_data = temp.dataProvider()
attr = layer.dataProvider().fields().toList()
temp_data.addAttributes(attr)
temp.updateFields()
temp_data.addFeatures(feats)

QgsMapLayerRegistry.instance().addMapLayer(temp)
# Adds memory layer with all copied attributes to ToC

Then calculate $area using the new memory layer.


I was unable to define IGNF:LAMB93 as the CRS so I did a test and saved an example layer using the same CRS. When I imported this back into QGIS and examinded the CRS definition, it was shown as a custom user CRS which used the same definition as EPSG:102110:

layer = qgis.utils.iface.activeLayer()
print layer.crs().authid()

Not sure if this would greatly affect your $area results.

| improve this answer | |
  • 1
    What do you think about to reproject all the vector files to a one CRS using ogr2ogr method and open them all in QGIS ? – Artec Jan 12 '16 at 21:36
  • @Artec - That sounds like a perfectly reasonable solution! I would encourage you to post that as an answer =) – Joseph Jan 13 '16 at 10:04
1

Second way is using ogr2ogr transformation for all your layers. You need to download open source GDAL tools and look at how to install it: Install GDAL

1. Open cmd console
2. Navigate to gdal folder (ex.) cd gdal_16\bin
3. Make dir for output (ex.) md Converted
4. Put all your files .shp, .dbf, .dbx to gdal_16\bin
5. Run command from cmd

FOR %i IN (*.shp) DO ogr2ogr -t_srs EPSG:Your_EPSG_code "Converted\%i" "%i"
where Your_EPSG_code is Lambert93 (ex. EPSG:2154)
6. Files reprojected to your CRS will be located in "Converted" folder.
7. Now you can use all of them to your project.

You can check your EPSG code here EPSG

I think that is possible to make batch file to do this automatically...but I cant help you with this.

Remember ! Try this on a copy of your files.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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