2

I want to select features from a shapefile where the value of column matches a regex then save only the selected features in a new shapefile.(I used regex101.com to write the regex and check if it's captured in the test string. So the regex is good). Here's what I did:

def filterByRegex(shapefile, output):

# Read the shapefile
shpfile = gpd.read_file(shapefile)

p = re.compile(
    ur'(^[\d]\shour\sparking\s[\d]am.[\d]pm\smon.fri\sexcept\sby\szone\s.*|^[\d]\shour\svisitor\slimit\s[\d]am.[\d]pm\smon.sat\sexcept\sby\szone\sk\spermit.*|^[\d]\shour\svisitor\sparking\s[\d]am.[\d]pm\smon.fri\sexcept\sby\szone.*|^visitor\sparking\stime\slimits\s[\d][\d]\smin\s.\s\[chart\]\s.\sexcept\sby\szone.*|^stickered\s-\svisitor\sparking\stime\slimits\s.\s\[chart\]\s.\sexcept\sby\szone\s[A-Z].*|^Sign.\s[A-Z][\d][\d][\d][\d].\sReserved\sfor\scarpools\swith\spermit\s[\d]am-[\d][\d]am\smon-fri.*|^Sign.\s[A-Z][\d][\d][\d][\d].\s[\d]\shour\sparking\s[\d]am-[\d]pm\smon-sat\sexcept\sby\szone\s.*|^Sign.\s[A-Z]444[\d].\sparking.\srectangle.\sgreen.white.\s.*)', re.MULTILINE | re.IGNORECASE | re.VERBOSE)

test_str = shpfile['Comments']
print test_str
res = re.findall(p, str(test_str))
print res #empty geodataframe?

# Save the selected features to a new shapefile
res.to_file(output)

filterByRegex('./Outputs/joinedInput.shp', './Outputs/output.shp')

When I run the script I get an empty geodataframe :

Name: Comments, dtype: object
[]
Process finished with exit code 0

I tried using shpfile.Comments.str.extract() and shpfile.Comments.str.contains(), but I get the same result.

Any suggestions?

1 Answer 1

2

I don't have your data to test the regex, but here is a simple piece of code that works with a sample data I have:

import geopandas as gpd
import re

def filterByRegex(shapefile, output):
    shpfile = gpd.read_file(shapefile)
    idx = shpfile.EDITBY.str.contains('^ERob', flags=re.IGNORECASE, regex=True, na=False)
    print shpfile[idx]
    shpfile[idx].to_file(output)

filterByRegex(r'C:\GIS\Temp\Roads.shp', r'C:\GIS\Temp\RoadsFiltered.shp')

The shpfile.EDITBY refers to the column name, in your case it would be Comments. The idx returns a Boolean index such as this:

0     False
1     False
2     True

Then we apply this index on the data frame to filter the rows.

Consider reviewing the pandas tutorials to learn more about working data frames, here are some really good ones: Lessons for New pandas Users.

3
  • Thank you again for ur answer @Alex! I tried what u suggested but I get an error stating : UserWarning: This pattern has match groups. To actually get the groups, use str.extract. idx = shpfile.Comments.str.contains(testregex, flags=re.IGNORECASE, regex=True, na=False) Empty GeoDataFrame Columns: [APPPEndDat, APPPStartD, APPPTimeDa, APPPZone, Arrow, Comments, LocationID, OBJECTID_l, OBJECTID_r, Rotation, RotationMe, SHAPE_Area, SHAPE_Leng, SignCode, SignType, VisitorLim, ZoneName, geometry, index_righ] Index: []
    – GeoSal
    Commented Aug 8, 2016 at 10:20
  • File "/usr/local/lib/python2.7/dist-packages/geopandas/geodataframe.py", line 343, in to_file to_file(self, filename, driver, schema, **kwargs) File "/usr/local/lib/python2.7/dist-packages/geopandas/io/file.py", line 58, in to_file schema = infer_schema(df) File "/usr/local/lib/python2.7/dist-packages/geopandas/io/file.py", line 87, in infer_schema raise ValueError("Geometry column cannot contain mutiple " ValueError: Geometry column cannot contain mutiple geometry types when writing to file.
    – GeoSal
    Commented Aug 8, 2016 at 10:22
  • 1
    @Gisel, this error is related to regex, look here stackoverflow.com/questions/22104466/… or Google more about it. There is something with the regex you usethat should be fixed. Try running first with a very simple regex to see if you get a workflow working first. Commented Aug 8, 2016 at 10:23

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.