0

I have the usual US census states shapefile for the 48 contiguous states. I am trying to select a state of interest from that shapefile then save that shapefile and display. I want to use GeoPandas, Shapely, Fiona to do this.

Any ideas on how this could be done?

closed as off-topic by BERA, PolyGeo Nov 20 '18 at 18:44

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body. Providing a clear problem statement and a code attempt helps others to help you." – BERA, PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • GeoPandas use Shapely and Fiona – gene Nov 20 '18 at 17:49
2

Hard to say without more info but like this:

import geopandas as gpd

states = gpd.read_file(path_to_shape)

#select alaska if column name is "State"
ak = states[states['State'] == 'Alaska'])

#plot
ak.plot()

#save
ak.to_file(driver = 'ESRI Shapefile', filename = out_path)
  • Perfect! Thank you so much! Only thing I am trying to do now is created an outpath to my desktop. Not sure what to_file(out_path) means exactly. I created a variable called out_path like so: out_path = r"C:\Users\new\Desktop\selection_state.shp"....and then at the end of that code snippet I wrote: selection.to_file(out_path). Gives me a RuntimeError and says Failed to open C:\Users\new\Desktop\selection_state.shp – yuen2 Nov 20 '18 at 18:03
  • try selection.to_file(driver = 'ESRI Shapefile', filename = out_path), I suggest you read geopandas.org as all your questions are answered in the documentation – Stefano Potter Nov 20 '18 at 18:13
  • If it's not asking too much. I have a related question. Would this be correct way to reproject that shapefile data to another projection? ak.crs = ({'init' :'epsg:3395'}), I want to put it in the best UTM but I don't think GeoPandas offers the option to input UTM projections like for example UTM 10 for California. – yuen2 Nov 20 '18 at 18:53
  • Everything has an EPSG, spatialreference.org/ref/epsg/wgs-84-utm-zone-10n is the one you want. What you have listed there is defining the projection, be careful that you don't actually need to reproject which would be ak = ak.to_crs({'init': 'epsg:3395'}), once again, this is in the documentation, geopandas.org/projections.html, please read. – Stefano Potter Nov 20 '18 at 19:30
  • Thanks once more. What I have has defined the projection, that means that my California shapefile is not in epsg: 3395 projection? Is defining the projection the same thing as reprojecting the shapefile? – yuen2 Nov 20 '18 at 20:06

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