Transfer 3D shapefiles “z” values to the table of attributes

3D shapefiles are not that common, but sometimes they appear. The 3rd dimension value is stored inside the geometries and usually does not show in the table of attributes. As most of the GIS software does not handle the 3rd dimension it is then necessary to transfer this value from the geometries to the table of attributes. Databases like PostGIS and Spatialite together with Quantum GIS come to help.

The Spatialte way:

  • Open QGIS, add your shapefile and save it as Spatialite (or alternatively use the QGIS DB Manager to drag and drop the shapefile to an already existing Spatialite Database)
  • Add the newly created Spatialite vector and through the QGIS vector properties (“fields” tab) add a new column, that will store the “z” values
  • In the QGIS DB Manager SQL Window use this command:

update tablename set columnname = st_z(st_pointn(geom,1)

The PostGIS way:

  • Import the shapefile to your PostGIS database

ogr2ogr -f “PostgreSQL” PG:”host=yourhost user=user dbname=dbname password=*****” shapename.shp

  • Connect to you database

psql -h yourhost -U user dbname

  • Add a column

ALTER TABLE tablename ADD COLUMN columnname numeric(19,11);

  • Fill the column with the “z” values

update  tablename  set columnname = st_z(ST_PointN(wkb_geometry,1));

About these ads