I am new to using PyCharm, but I am trying to

  1. cut out 4 rows from a county shapefile

  2. from that 4 rows specify the columns or attributes to also be extracted

  3. 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\\20test\\counties\\counties.shp') as input:

  output_schema = input.schema.copy()

  with fiona.open(C:\\Users\\Desktop\\GEM\20test\\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']})
  • Placing all your code in a try block is a great way to hide useful error messages. I suggest you remove the try...except and report the actual error encountered. – Vince Mar 24 '20 at 3:03
  • There are a lot of wrong indentations in your code. Please fix them first. – Kadir Şahbaz Mar 24 '20 at 5:00
  • i have fixed the indentation please can you just check – rachel Mar 24 '20 at 14:56
  • 1
    Your file paths are not formatted correctly. Take a look as this SO answer for how to format them. – Ryan Mar 24 '20 at 15:14
  • ryan, thanks, i have rectified the file path but am still getting an error, and my major problem is how to remove specific row and output it as a shapefile – rachel Mar 24 '20 at 15:29

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\\GEM\20test\\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}

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\\20test\\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

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\GEM\20test\venv\lib\site-packages\geopandas\io\file.py", line 106, in read_file assert len(bbox) == 4 AssertionError – rachel Mar 24 '20 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 Mar 24 '20 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 please – rachel Mar 24 '20 at 21:38
  • I'm not sure if and how you can do this with just Fiona and Shapely, sorry. – Hans Roelofsen Mar 25 '20 at 7:52

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.