2

I have a shapefile for the provincial boundaries of Canada obtained from here

It is a single shapefile with all of the provincial boundaries inside of it. I want to just extract a single province from it, and use that as it's own shapefile elsewhere (ie. ontario.shp, quebec.shp, etc)

Is there a way to do this?

enter image description here

4

You select the chosen feature and then choose Layer/Save as... (you let the checkbox 'save only selected features' checked) and you're done ...

Right Click on the layer + Export does the job too.

You can also choose Save selected feature as ... and enter the table name.

An other nice option is to let the original layer unchanged and only filter it to keep the expected feature. (Layer properties/Source/Query builder)

2

This is a very nice use of ogr2ogr spatial SQL queries. You can even do the extraction without downloading and unzipping the file.

ogr2ogr -dialect sqlite \
  -sql "select * from lpr_000b16a_e where PRENAME='Quebec'" \
   quebec.shp \
   /vsizip/vsicurl/http://www12.statcan.gc.ca/census-recensement/2011/geo/bound-limit/files-fichiers/2016/lpr_000b16a_e.zip

In this particular case, you don't need a spatial query, but just a where statement on the command line:

ogr2ogr -where "PRENAME='Quebec'" \
   quebec.shp \
   /vsizip/vsicurl/http://www12.statcan.gc.ca/census-recensement/2011/geo/bound-limit/files-fichiers/2016/lpr_000b16a_e.zip
1

I'm a total beginner, so I'll add a visual to help out in case anyone else needs it.

  1. Select "Select Features" from the top toolbar
  2. Select your feature
  3. Right click on the layer in the layer panel and select "Export > Save Selected Feature As"

enter image description here

1
  • 1
    You can also right click your table in the left pane called "Table of Content" then "Open Attribute Table" and select feature from rows there or even based on a filters or conditions (browse the menu items within the attribute table to try them! It's worth a try.) – s.k May 3 '20 at 8:55
0

if you want to separate all the polygons you can do it by running the script. which separates all the polygons turning it into new shape

from osgeo import ogr

input_shape = r"C:\Users\manuel\Documents\INPUT\Geol.shp"
salida      = r"C:\Users\manuel\Documents\OUTPUT"
driver      = ogr.GetDriverByName('ESRI Shapefile')
data_source = driver.Open(input_shape)
layer       = data_source.GetLayer()
sr          = layer.GetSpatialRef()
new_feat    = ogr.Feature(layer.GetLayerDefn())
# Loop over features:
for id, feat in enumerate(layer):
    new_ds  = driver.CreateDataSource(r"{}\feat_{}.shp".format(salida, id))
    new_lyr = new_ds.CreateLayer('feat_{}'.format(id), sr, ogr.wkbPolygon) 
    geom    = feat.geometry().Clone()

    new_feat.SetGeometry(geom)
    new_lyr.CreateFeature(new_feat)
    del(new_ds, new_lyr)

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.