I am new to using PyCharm, but I am trying to
cut out 4 rows from a county shapefile
from that 4 rows specify the columns or attributes to also be extracted
and output the result as a new shapefile. but when I try this code, it gave me some errors, please any ideas?
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
new error after rectifying file path SyntaxError: positional argument follows keyword argument
import gdal_workaround
import fiona
import shapely
if __name__ == "__main__":
with fiona.open('C:\\Users\\Desktop\\GEM\20円test\\counties\\counties.shp') as input:
output_schema = input.schema.copy()
with fiona.open(C:\\Users\\Desktop\\GEM20円test\\counties\\counties2.shp , 'w', 'ESRI Shapefile',output_schema, crs=input.epsg, 4326) as output:
for elem in selection:
output.write({'properties': elem['nut2name'],'geometry': elem['polygon']})
2 Answers 2
What you are trying to do can indeed be done with Fiona and Shapely.
for this error:
SyntaxError: positional argument follows keyword argument
The problem is in your fiona.open
call here:
with fiona.open(r'C:\\Users\\Desktop\\GEM20円test\\counties\\counties2.shp' , 'w', 'ESRI Shapefile',output_schema, crs=input.epsg, 4326) as output:
You have 4326
in the function call after specifying crs=input.epsg
. Since you're specifying the CRS as the same as the input file, you don't need the 4326
there. This isn't an error with fiona necessarily but something that's not allowed in Python:
def my_func(thing1, thing2):
print(thing1, thing2)
my_func(thing1='hello', 'world')
SyntaxError: positional argument follows keyword argument
Alright, now to the meat of the issue. You want to "select" certain rows from your input shapefile. You can do that easily, you just need to look at the values of the columns you want to "select" using in each row of the shapefile. In fiona
, the records are converted to GeoJSON structure, so you can access attributes in the properties
member of the record. Imagine we have a shapefile of counties with three columns, the name, the fips, and the population, and we want to "select" the counties where population is greater than 100,000 people. We only want to write out the name and the population.
# open our input file
src = fiona.open('counties.shp')
# define the columns - by name - which we want to
# keep from the original file
keep_columns = ['name', 'population']
# create the output schema from the input
output_schema = src.schema.copy()
# create new properties schema without the columns we don't want
output_schema['properties'] = {column_name: typ for column_name, typ in output_schema['properties'].items() if column_name in keep_columns}
# Open output file
sink = fiona.open('counties_over_100k.shp', 'w', driver='Esri Shapefile', schema=output_schema, crs=src.crs)
for feature in src:
# check to see if this is a row we care about based
# on the value of a column
if feature['properties']['population'] > 100000:
# remove columns we don't need from the feature
feature['properties'] = {column_name: value for column_name, value in feature['properties'].items() if column_name in keep_columns}
sink.write(feature)
Example code using Geopandas to select rows and columns from a shapefile and write the result again as shapefile. The row selection is based on a hypothetical attribute 'county_name'. The column selection is also for hypothetical attributes. You can modify these to your needs.
import geopandas as gp
# read file
counties = gp.open(r'C:\\Users\\Desktop\\GEM\20円test\\counties\\counties.shp')
# select rows
counties_row_sel = counties.loc[counties['county_name'].isin(['County1', 'County2', 'County3', 'County4']), :]
# select columns
counties_row_col_sel = counties_row_sel.loc[:, ['county_name', 'attribute1', 'attribute2', 'attribute3', 'attribute4']]
# write to file
counties_row_col_sel.to_file('Counties_Sel.shp')
Geopandas uses the well-known Pandas data analysis and management library to deal with the shapefile attribute tables. See here for common Pandas data selection procedures.
-
hi i tried this code but it returned this error Traceback (most recent call last): File "C:\Users\Desktop\GEM20円test\venv\lib\site-packages\geopandas\io\file.py", line 106, in read_file assert len(bbox) == 4 AssertionErrorrachel– rachel2020年03月24日 20:09:11 +00:00Commented Mar 24, 2020 at 20:09
-
That's interesting. It suggests the extent of your shapefile is not composed of 4 coordinate pairs. Does your shapefile open properly in a desktop GIS (QGIS, ArcMap, ArcGIS Pro)?Hans Roelofsen– Hans Roelofsen2020年03月24日 21:09:38 +00:00Commented Mar 24, 2020 at 21:09
-
yes it does on ARCMAP, but i have one question: is it possible for this process to be done using fiona and shapely pleaserachel– rachel2020年03月24日 21:38:41 +00:00Commented Mar 24, 2020 at 21:38
-
I'm not sure if and how you can do this with just Fiona and Shapely, sorry.Hans Roelofsen– Hans Roelofsen2020年03月25日 07:52:58 +00:00Commented Mar 25, 2020 at 7:52
try
block is a great way to hide useful error messages. I suggest you remove thetry...except
and report the actual error encountered.