8

I have to merge a lot of shapefiles into one great shapefile.
So I want to compare the properties of the attribute tables to see if there are differences between the tableproperties of each shape (type, length, precision, name, etc). With the intention to standardize all the shapefiles

eg:

enter image description here

Is there a way to print out a list of the table properties as shown in the tab 'fields' or to export the properties into an .xls or .dbf?

Or is there an automated way to do the standardisation? (Perhaps this is a subject for a new thread...)

I am using QGIS 1.8 (QGIS 2.2 is also possible).

Thanks

  • Indeed, I meant print it on paper. But if you can suggest me how to do it in an easier way. Please tell me. – PieterB Feb 26 '14 at 12:30
  • I could only find answers about Arc but it should work similar for QGIS: gis.stackexchange.com/questions/54034/… – tobias47n9e Feb 26 '14 at 15:20
  • Can you elaborate a bit more what you mean by "properties of the attribute table"? Preferable a list of properties which you would like to compare - and how you would like to compare them. What will be the end result of your analysis? – Matthias Kuhn Feb 27 '14 at 11:54
  • @ Matthias: I added some more information in my answer – PieterB Feb 27 '14 at 13:12
  • @Spießbürger I've refined further my answer providing also a Processing script solution. Hope this helps. – Antonio Falciano Mar 7 '14 at 8:51
7
+50

OSGeo4W shell solution

IMHO the simplest way to extract the attribute table properties (schema) consists into opening the OSGeo4W shell ('cause you're on win os), change directory to your data folder and simply type something like:

ogrinfo -so inputLayerName.shp inputLayerName

It will show you the summary information like projection, schema, feature count and extents. Then, because you have a bunch of shapefiles, you can do a FOR cycle like the following:

FOR %f IN (*.shp) DO ogrinfo -so %f %~nf >> properties.txt

This returns one txt-file with the properties of all the shapefiles in a directory (because the output redirection with >> appends each single output to the properties.txt file).

Alternatively, if you're interested in one properties file for each shapefile:

FOR %f IN (*.shp) DO ogrinfo -so %f %~nf > %~nf_properties.txt

About the standardization, there are several techniques. I suggest you to use the RESIZE layer creation option after the merge in order to resize fields to their optimal size (e.g. text fields with excessive length will be shortened). For instance:

ogr2ogr -lco RESIZE=yes merge_resized.shp merge.shp 

Processing script solution

Open the Processing toolbox in QGIS, create a new script (clicking on Scripts --> Tools --> Create new script) and type:

##ogrinfo (summary only)=name
##input=vector
##output=output file

import os, subprocess

head, tail = os.path.split(input)
inputname = os.path.splitext(tail)[0]
cmd = 'ogrinfo -so ' + input + ' ' + inputname + ' > ' + output
subprocess.check_call(cmd, shell=True)

Save it as you like, e.g. ogrinfo_so.py. Then, a new script ogrinfo (summary only) will appear in the Processing toolbox --> Scripts --> User script group. It can be executed as it is or in batch mode.

The same operation is possible in order to resize the field length, as described before:

##Resize fields=name
##input=vector
##output=output vector

import subprocess

cmd = 'ogr2ogr -lco RESIZE=YES ' + output + ' ' + input 
subprocess.check_call(cmd, shell=True)

A new script called Resize fields will be available in the Processing toolbox --> Scripts --> User scripts group. Enjoy it!

  • Interesting! And how can i get the info out of the OSGeo4W shell into a text- or excelldocument? ps: is there beginnersdocumentation on how to use that OSGeo4W shell? – PieterB Mar 5 '14 at 13:37
  • The OSGeo4W Shell is a batch / DOS Shell, so you can select the text, copy it and paste into a text file – Antonio Falciano Mar 5 '14 at 13:47
  • 1
    edit was made by @PieterB (thanks to him). I just corrected a typo. – simo Mar 6 '14 at 14:37
  • 1
    @afalciano: Thanks for introducing me into the world of OSGeo4W. There is still a lot to learn... – PieterB Mar 6 '14 at 14:45
4

Shapefiles hold their attributes in .dbf files, can you not just open the .dbf file directly in a program like libreoffice calc and then print from there? If you don't have too many files this should work.

Another option would be to write a python script that prints out (to the console) the attributes of features directly, or even compares them for you. You can access the attributes of a feature like this:

it = vectorLayer.getFeatures()
for eachFeature in it:
    attributes = eachFeature.attributes()

You would need to do this for each shapefile your comparing and see if

eachFeature.attributes() == eachOtherFeature.attributes()

more info here: http://www.qgis.org/en/docs/pyqgis_developer_cookbook/

  • If more people would be interested in a tool like this perhaps we could crowd-fund the development. – tobias47n9e Feb 27 '14 at 15:29
  • That sounds like an interesting idea – user24956 Feb 27 '14 at 15:36
  • They should have a form for that on the QGIS website :) – tobias47n9e Feb 27 '14 at 16:25
1

Following on the .dbf theme have you looked for .dbf comparers directly? I haven't found a free solution immediately, but something like: dbfCompare

1

Try some of the DBF metadata extractors available. Most of them support command line mode, so multiple metadata extraction can be run in batch.

http://geology.usgs.gov/tools/metadata/tools/doc/dbfmeta.html http://sco.wisc.edu/wisclinc/metatool/dbfmeta.htm

0

Just open the .dbf file in Libreoffice Calc or Excel just save it as a new file. I saved over it once and the shapefile was no good.

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.