QGIS mapping
Published: 2022-07-06 07:06:03


## Introduction


For our second group project in my Data Science course - we were tasked with designing a linear regression model that would predict housing prices.


For this analysis, we were provided with the dataset  "King County Housing Price from May 2014- May 2015" created by the Center for Spatial Data Science (https://spatial.uchicago.edu). It contained 21,597 entries with 21 different columns about home sales in the Seattle metropolitan area. One of the columns I found pretty interesting was the waterfront property feature. 

Upon examining the data - it seemed like the waterfront feature wasn't capturing all the homes it should have caught. There were a lot of null values. So rather than dropping the feature entirely - I thought there had to be a better way of bringing that information out.

So I came upon QGIS. QGIS is a "Free and Open Source Geographic Information System [that enables users to] Create, edit, visualise, analyse and publish geospatial information on Windows, Mac, Linux, BSD and mobile devices"

### Here's a brief overview of the steps:

1. Download and install QGIS.
2. Load map 
3. Input latitude and longitude for each home sale point
4. Overlay shape file
5. Calculate distance from homes to body of water
6. Export results.


### Step 1 - Install QGIS
Download and install QGIS. QGIS can be installed from [](https://www.qgis.org/en/site/). 

### Step 2 - Load map
When you first open up QGIS - it's just a white screen. You have to actually load a map. On the left side of the screen you'll see a few different options - I chose the "Street Map" option under the XYZ tiles header.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r3pz6ag06m8atwzoy1xx.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ukx8nmy0tksng95voscc.png)


Since the data we'll be importing is from the Seattle metropilitan area make sure you navigate and zoom to the pacific northwest.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wa6mcor30n34k2ajqxtw.png)




### Step 3 - Input Latitude and Longitude
At the top of the screen there are more options. Navigate to Layer > Add Layer > Add delimited Text Layer. In the box that comes up you'll need to import your data file and label your lat and long as x and y.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7slh1hogl87j4sv6ev34.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lbvccmg725ww5318qxu7.png)


Your map should now look like this.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tvw1iutf0c99ghv1dkvl.png)


### Step 4 - Overlay shape file
Our next step is to map the bodies of water in the area. For this project the shape file gives the 'shape' of the bodies of water in a given area. I used the Seattle dataset downloaded from ([](https://www.usgs.gov/national-hydrography/national-hydrography-dataset). 
Now we just add the shapefile as a layer onto our map. You can just drag it from your download folder onto the map itself.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/38analqchtl3j0kksnyy.png)




now our map look likes this - the bodies of water are dark blue - note you can change the colors of the map by playing around with the color settings on the right side of the screen.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u2xxnkc7pbot3zuu05zt.png)



### Step 5 - Calculate distance 
Now this step has a few parts - first we need to find the shoreline, then calculate the distance from homes to the closest body of water.

In the lower right hand corner - there is a search bar - we need to find and run the "Convert polygon/line vertices to points”. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z5h3um0t6ihnow4oamym.png)

Since we are interested in finding the shorelines make sure its running on the waterbody shape file you downloaded.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iql1oa6df1rfxhk8tj36.png)


Now we've added the shorelines onto our map (yellow)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nn3tdanwavf4ry1w9z3e.png)


Next we need to calculate the distance in miles (denoted by the orange line) from each home point (purple) to the closest shoreline (yellow).
In that same lower right hand corner we need to find and run the "Distance to nearest hub (line to hub)”. The source layer - is our housing location. The destination layer is the points layer. For distance I used miles but feel free to use another measurement


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tu2xuyyj3v7bh1gonmz6.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tnvesmmaykk5tx47emvs.png)

After running the distance tool this is what we now have


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5vz4kj96q0wj5uk8uj3z.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/illcrnr3athti2z26uxt.png)


### Step 6 - Exporting data
Our final step is to add our distance data to our original data and export it out of QGIS.

In the right hand corner double-click our housing data layer, select joins from the pop-up, add a join (the green plus sign). 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qjav21dj46i6zsk14xu2.png)

Use distance for join layer, and id for join field and target field. Make sure the joined fields box is checked and select the distance feature and hit ok.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3o4ty933v49r26jbgo5m.png)


Finally export. 
Right click on the data layer again, and export.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ybyyc50l41gosid6q1pr.png)


**And just like that. We're done.**