2

i have made a new shapefile from the original shapefile , and now i want to copy the attribute table from the original shapefile to the new select shapefile , but i don't know how to do that?

import ogr,os,sys,ogr

Datasource_Parks = 'ArcCR500_v32/ChranenaUzemi.shp'

Direction_Result_Shp = 'ArcCR500_v32/Result.shp'
Direction_Result_Dbf="ArcCR500_v32/Table.dbf"


Open_Parks = ogr.Open(Datasource_Parks)
layer_park = Open_Parks.GetLayer(0)

SQL = "NAZEV_ASCI = 'Krkonossky narodni park'"

layer_park.SetAttributeFilter(SQL)

Number_Of_Park = layer_park.GetFeatureCount()

Driver = ogr.GetDriverByName("ESRI shapefile")

if os.path.exists(Direction_Result_Shp):
    Driver.DeleteDataSource(Direction_Result_Shp)

Dataset = Driver.CreateDataSource(Direction_Result_Shp)

New_Layer = Dataset.CreateLayer(Direction_Result_Shp, geom_type= ogr.wkbPolygon)

number = 0
for i in range(0, Number_Of_Park):
    H = layer_park.GetNextFeature()
    H_geometry = H.GetGeometryRef()

    New_Layer.CreateFeature(H)

Dataset.Destroy()

attribute table form the original shape file,"ChranenaUzemni.shp

New shape file with no attribute table

2
  • You can't do that directly. What you need to do is Join by Attributes, hide the fields you don't want, and 'save as' a new shapefile. For this to work you must have a unique field matching between tables - it can be numeric (integer) or text but shouldn't be float/double. Commented Dec 14, 2015 at 23:10
  • Why just you dont use selection of your feature and then use datasource.CopyLayer and copy the feature into the new shapefile?
    – david_p
    Commented Dec 15, 2015 at 7:45

2 Answers 2

3

if you wanted to stick with the loop (maybe to modify attribute names in some way) - you could also do something like:

    #after creating New_Layer
    featdef = ogr.Feature(layer_park.GetLayerDefn())
    feat1 = layer_park.GetNextFeature() #read first feature to get attributes
    for i in range(feat1.GetFieldCount()):
        fieldDef = feat1.GetFieldDefnRef(i)
        New_Layer.CreateField(fieldDef)

    layer_park.ResetReading() #back to first feature of original layer
    for feat in layer_park:
        New_Layer.CreateFeature(feat)
        feat = None

note - you don't have to pull the geometry separately

1

I have made some edits in your script. I changed the For cycle into Copy Layer method. Try it if you want. This method preserve the attributes as you want.

import ogr,os,sys

Datasource_Parks = 'ArcCR500_v32/ChranenaUzemi.shp'

Direction_Result_Shp = 'ArcCR500_v32/Result.shp'
Direction_Result_Dbf="ArcCR500_v32/Table.dbf"

Open_Parks = ogr.Open(Datasource_Parks)
layer_park = Open_Parks.GetLayer(0)

############################## add variable 'location' into SQL
location = 'Krkonossky narodni park'
SQL = "NAZEV_ASCI = " + locaion

layer_park.SetAttributeFilter(SQL)

Number_Of_Park = layer_park.GetFeatureCount()

############################## edit
####### make output variable (only first word)
output_file = "output_" + location.split(" ")[0] + ".shp"

driver_out = ogr.GetDriverByName("ESRI Shapefile")
ds_out = driver_out.CreateDataSource(output_file)

lyr_out = ds_out.CopyLayer(layer_park, output_file.split(".")[0])

lyr_out.SyncToDisk()
ds_out.SyncToDisk()
############################## end


Dataset.Destroy() 
7
  • thanks for your help,but there is problem when i run the script, for the first time it works but when i run it again, i faced with this error:AttributeError:'NoneType'object has no attribute 'CopyLayer', do you know have can i solve this.
    – Saeed
    Commented Dec 15, 2015 at 10:56
  • You have to make "output_file.shp" as a variable. You cant create the same layer again, thats because varable is necessary. I have made some changes in script above.
    – david_p
    Commented Dec 15, 2015 at 11:28
  • Am I right that you want to export your location one by one to new shapefile?
    – david_p
    Commented Dec 15, 2015 at 11:29
  • so let me tell you my task. i have a shape file with the two type of name(National park and park in CR) and also a dem file . so i have to first select the type one(National park) and then with the dem file (Zonal Statistics as table) create a .dbf table . so from this table i have to select the park with the highest Range. so after that i should export just this layer(with the highest Range) with all the original attribute. so if you can help about the steps?
    – Saeed
    Commented Dec 15, 2015 at 11:47
  • Can you use arcpy module? And do you that workflow to be automatic?
    – david_p
    Commented Dec 15, 2015 at 11:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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