Monday, December 6, 2010

How to calculate the length value for a field in QGIS

Really I should have titled this article. "How to create a shapefile with a field to store length values and then fill the field with length values using Quantum GIS aka.  QGIS "
- Using Quantum GIS 1.7 r14693  from the osgeo4w-setup.exe
- OS  Windows 7 Professional 32 bit
- example will use EPSG 31258 but you can of course use your own local EPSG code.
- Generally I already have some other data in EPSG 31258 I use as background data so I can see where my new file is in the world.  When QGIS starts it has a default location of 0.00 x  and  0.00 y at the center of your map window which = no where in particular.  You should then define your project coordinate system under Settings >  Project Properties > tab coordiate reference systems and set to EPSG 31258.
For more funky spatial reference EPSG  fun check out

Part 1. Creating an empty Shapefile to store our data with lengths.
  1. Open Quantum GIS Go to Layer > New > New Shapefile Layer…
  2. Select the radio box   Line
  3. Select your local coordinate system for us here in Klagenfurt, Austria we are going to use EPSG 31258 (ESRI “MGI_Austria_GK_M31″).
  4. Add a new attribute field one called id.  (this can later be used as a primary key value)
    Name:  id
    Type: whole number
    Length: 10  (default)
  5. Click   Add to attributes list
  6. Add a new attribute field one called length.  (here we will store our length value)
    Name:  length
    Type: real
    Length: 10  (default)
  7. Click Add to attributes list
  8. Click OK      Save As menu pops up and now give your shapefile a name and a location to save it on your computer.  Enter filename:  examplename
  9. Click SAVE

Part 2.  Editing our newly created shapefile and then finally populating the length field with values.

  1. Select your newly created shapefile in the Layers by clicking on the name (this will highlight the name in blue on windows 7 pro default settings)
  2. Click the Toggle editing button in the editing toolbar (the one that looks like a pen)
  3. Click the Capture line button and start clicking to draw your line  (left click, left click , left click ,left click … Right Click = DONE)
  4. Click the Open Attribute Table button
  5. Click the Open Field calculator button (looks like a calculator)
  6. Select update existing field
  7. select the length field
  8. select the button called Length with a double click  (now $length  should show up in the field calculator expression box)
  9. Click OK
 and wolla! you now have some funky length data in your length field !  only x clicks and you have it.
If you have any feedback please let me know.
Unitl next time

No comments:

Post a Comment