Step-by-step tutorials for working on mapping projects using GIS methods and software.
Clipping is a way of cropping GIS data to a certain extent.
In this tutorial you will clip a shapefile of all census tracts in the United States to create a new shapefile of only census tracts in Cambridge, Massachusetts. For those who are following the census data tutorial series, we will be using the United States Census Tracts file we just created. For those not using the tutorial, you should download the U.S. Census Tracts file.
Because we are clipping census tracts data to the extent of Cambridge, Massachusetts, you will also need to download a boundary file for the City of Cambridge, MA..
Before we clip by geography, let’s first isolate only census tracts in the state of Massachsuetts. This will make our data much easier to work with, and will make the clipping processes run faster.
Open QGIS.
Add the census tracts file titled TRACT_2019_US_SL140_Coast_Clipped.shp
to QGIS.
A predominant spatial data format is the shapefile. This is a format created for storing vector data.
Vector data consists of:
Since the advent of geospatial technology, shapefiles have been the most common format for storing vector information. Today, other file formats exist for storing vector information, such as the geopackage (.gpkg), or geoJSON (.geoJSON), but shapefiles are still widely used, and many of the datasets you will encounter will come in this format.
If the quick add approach is not cooperating, you can add data through the menu add.
Open Attribute Table
.
Filter the table to only show records that have a state FIPS code of 25, the Massachusetts state FIPS code.
In the bottom-right hand corner click Show All Features
.
Select Field Filter → STATEFP
.
In the search box to the right of STATEFP
type in 25
.
Press the enter key
. It will take a long time to return results because there are so many records to search through.
Any feature you have selected will appear yellow on the map to indicate it is highlighted. All of Massachusetts should be highlighted.
To save just the Massachusetts tracts as a new file, right click on the tracts layer in the layer list and select Export → Save Selected Features As
.
QGIS may default to other vector file formats, such as geopackage (.gpkg) or shapefile (.shp). Any of these formats will work for storing vector data.
Select OK
.
Remove layer
.Check to make sure your project looks something like this:
Add an opacity slider to the Cambridge boundary layer by following the steps in this tutorial. Use this to “peer under” the Cambridge boundary data and inspect the tracts we will be isolating via the geographic clip.
In the main QGIS menu (banner across the top of the computer screen), select Vector → Geoprocessing Tools → Clip
.
- Input: Data you want to clip
- Output: Clipping boundary (new extent you want to clip by)
Clipped
select the ellipse three dots icon, pick Save to file
, and save the new clipped layer somewhere you will remember. You can title the file Cambridge-tracts
, and save it as either a shapefile
or geoJSON
.
Tip: We prefer GeoJSON because it is an open standard, and is only one file instead of six.
Select Save
.
Select Run
.
Tip: Any geoprocessing functions can fail if data layers are set in different coordinate reference systems. If you suspect this might be a problem, you can follow the steps in this tutorial.
Clipping by geography is the most straightforward way to clip one dataset to a specific extent. This method, however, can be a bit crude sometimes, and you may need to take other approaches for a more refined result.
In this example we will explain why a simple clip may fall short for your purposes.
The image above pictures two datasets: (1) buildings and (2) the border or outline of the city of Cambridge.
When we run a simple clipping function, that process crudely crops one dataset by the other. Here we can see how there are some buildings which straddle the border of Cambridge and the next town over, Somerville. The features for those buildings are being cut off halfway. Sometimes a simple clip will work, but sometimes you may need to use other, more nuanced toolbox functions.
Here, we have used the toolbox function Extract by location
. This function lets you specify how you want the data to be clipped. For instance, you can choose to extract all building features that fall within the boundary of Cambridge, as is pictured in the image above.
The image above pictures the extraction options. For instance, you can extract all data that falls within, intersects, contains, or overlaps the other dataset.
To use the extract by location tool, navigate to Processing → Toolbox → Vector Selection → Extract by Location
.
Under Extract features from
select the dataset you want to clip.
Under By comparing features from
select the dataset you want to clip by (the desired extent).