Skip to main content
deleted 9 characters in body
Source Link
david_p
  • 1.8k
  • 1
  • 19
  • 34

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 (jen prvni slovoonly zfirst lokaceword)
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() 

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 (jen prvni slovo z lokace)
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() 

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() 
added 172 characters in body
Source Link
david_p
  • 1.8k
  • 1
  • 19
  • 34

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 = 'Krkonossky" narodni+ park'"locaion

layer_park.SetAttributeFilter(SQL)

Number_Of_Park = layer_park.GetFeatureCount()

##################################################################################### edit
####### make output variable (jen prvni slovo z lokace)
output_file = "output_" + location.split(" ")[0] + ".shp"

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

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

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


Dataset.Destroy() 

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)

SQL = "NAZEV_ASCI = 'Krkonossky narodni park'"

layer_park.SetAttributeFilter(SQL)

Number_Of_Park = layer_park.GetFeatureCount()

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

lyr_out = ds_out.CopyLayer(layer_park, "output_file")

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


Dataset.Destroy() 

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 (jen prvni slovo z lokace)
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() 
Source Link
david_p
  • 1.8k
  • 1
  • 19
  • 34

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)

SQL = "NAZEV_ASCI = 'Krkonossky narodni park'"

layer_park.SetAttributeFilter(SQL)

Number_Of_Park = layer_park.GetFeatureCount()

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

lyr_out = ds_out.CopyLayer(layer_park, "output_file")

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


Dataset.Destroy()