9

I have a Shapefile layer in QGIS 2.6 with several polygons, each of which has data in over 100 fields. I need to create a new layer with all the same polygons, but with all their data fields blank (set to 0, Null or empty depending on the field type). Is there a quicker way of doing this than pressing delete on each field of each polygon one at a time, I would have to do this more than 1000 times this way.

  • 1
    Please edit the question to include the GIS software version and the data format (e.g., shapefile, FGDB, PostGIS,..) – Vince Jul 29 '15 at 11:18
3

Can't you just copy-and-paste your data into Layer X (a layer/shapefile/feature class/whatever) that has no attributes (besides OID and Geometry) and then copy-and-paste the now "empty" geometry back into your original layer?

6

You can enter the following code in the Python Console to clear ALL attributes to NULL for a shapefile loaded into QGIS. Select the layer from the layers panel (Table of Contents) and run the code:

layer = qgis.utils.iface.activeLayer()     
layer.startEditing()   
for field in layer.dataProvider().attributeIndexes():   
    for feature in layer.getFeatures(): 
        layer.changeAttributeValue(feature.id(), field, NULL)    

layer.commitChanges()        

This was tested on QGIS 2.8.2.


UPDATE:

In response to the comment by @Vince, the following code can be directly copied/pasted into the Python console and will change the values of attributes depending on the type of field (i.e. 0 for integer fields; NULL for string fields; and an epoch of 1900-01-01 for a Date field):

layer = qgis.utils.iface.activeLayer()     
layer.startEditing()  
for field in layer.pendingFields():
        if field.typeName() == 'Integer':
                name_int = field.name()
                for feature in layer.getFeatures():
                        feature[name_int] = '0'
                        layer.updateFeature(feature)
        if field.typeName() == 'String':
                name_str = field.name()
                for feature in layer.getFeatures():
                        feature[name_str] = NULL
                        layer.updateFeature(feature)
        if field.typeName() == 'Date':
                name_dat = field.name()
                for feature in layer.getFeatures():
                        feature[name_dat] = '1900-01-01'
                        layer.updateFeature(feature)
layer.commitChanges()
  • 1
    Can you tweak the logic to assign zero to numerics, blank/NULL to strings, and the epoch to dates? – Vince Jul 29 '15 at 13:04
  • @Vince - Thanks buddy, I can certainly try as I am no expert in this matter :) – Joseph Jul 29 '15 at 13:08
  • @Vince - Thank you again for mentioning the tweak, I happened to learn more about python =) – Joseph Aug 7 '15 at 14:38
  • 1
    I guess my age shows when I think of an epoch of Jan 1, 1970 or 1900, not 2000 ;) – Vince Aug 7 '15 at 14:58
3

You can simply save it as a new file and tick the "Skip Attribute Creation" box in the "Save as..." dialog. It does exactly what it says.

There will be one numeric counter column named FID, apparently the Shapefile format requires at least one attribute? If you can, use something better, spatialite or geopackage.

  • +1, you're right as there must be atleast one attribute. But this is a very nice and simple method as you could run the Field Calculator and replace all values of the FID to NULL. – Joseph Jul 31 '15 at 11:28
  • 1
    QGIS behaviour seems weird. The column FID is editable and it is not the actual mapping between geometry and attributes. If you delete the column, you get one named "feature id" which is not editable. That should be the first result already. QGIS 2.10. I will file a bug later. – bugmenot123 Jul 31 '15 at 12:00
2

Open the .dbf file with openoffice or others, and remove the data.
You can use python to loop over the files and remove all but the headers See dbf python module

  • 1
    Using shapefile-ignorant tools on the dBase file component is likely to cause shapefile corruption. If the purpose is to populate blanks and zeros, "removing" the data will likely only produce a dBase file with fewer records than the .shp/.shx. – Vince Jul 29 '15 at 12:46
  • Indeed, but we are simply remove the data, there isn't any risk. – julsbreakdown Jul 29 '15 at 12:57
  • No, the task is to preserve polygons and clear out the corresponding attributes, so the risk is very high. Any answer that does not warn of potential corruption does not take the risk seriously enough. – Vince Jul 29 '15 at 13:01
  • I recognize it is a hard way i.e. not a regular one but it works. Obviously, it is welcome to keep a backup of the original data. – Leehan Jul 29 '15 at 13:05
  • In this special case the risk is not so great because all the attributes in all the fields are initialized. Therefore if the order of records is changing in the dbf file it won't still make any mess because all the records have same data in attributes. User must still take care that the number of features in .dbf is the same as in .shp. However, as @Vince wrote, users should be warned that a) changing the order of rows of b) number of rows in .dbf is generally leading to a corrupted and unusable shapefile. – user30184 Jul 29 '15 at 13:13
2

QGIS can open shape files without the .dbf.

So you could just delete the .dbf component and load in the .shp, which will bring in just the geometry.

  • That's a good tip although I personally refrain myself to not deleting any associated files :) – Joseph Jul 31 '15 at 11:37
  • 1
    Yes, renaming might be a safer option. – HeikkiVesanto Jul 31 '15 at 11:38
2

Perhaps you can edit the *.dbf file with Excel.

Then you delete all fields but the geom one.

Finally save and quit.

It would be useful to keep an archive of the *.shp before doing this.

  • The meaning seems to be to keep the schema intact so only the values of the attributes should be set to some default value. And you can't save into dbf format with Excel. – user30184 Jul 29 '15 at 11:30
  • 2
    The geometry field doesn't exist in the dBase component of a shapefile. – Vince Jul 29 '15 at 12:48
  • right there s no wkt field when editing dbf with excel. – Leehan Jul 29 '15 at 13:02
  • The FID field links the rows in the .dbf to the geometry, which is stored in the .shp. Really, the .dbf has nothing to do with geometry. en.wikipedia.org/wiki/Shapefile – mr.adam Jul 29 '15 at 15:15
1

In QGIS 2.8.x you can also get rid of specific or all fields. Open attribute table, Toggle editing mode, Click on "Delete column" button, Select all your unwanted attributes and Click "OK". Save edits.

Your Answer

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