How to Make Maps Using Mapping Software (QGIS)
       |           

Welcome, Guest. Please login or register.
Did you miss your activation email?
March 28, 2024, 08:57:41 AM
News: Election Simulator 2.0 Released. Senate/Gubernatorial maps, proportional electoral votes, and more - Read more

  Talk Elections
  About this Site
  Talk Elections Q & A
  How To (Moderator: Virginiá)
  How to Make Maps Using Mapping Software (QGIS)
« previous next »
Pages: 1 [2] 3
Author Topic: How to Make Maps Using Mapping Software (QGIS)  (Read 22325 times)
cinyc
Atlas Icon
*****
Posts: 12,721


Show only this user's posts in this thread
« Reply #25 on: November 03, 2017, 10:05:25 AM »
« edited: November 03, 2017, 02:32:37 PM by cinyc »

After you added labels, I would do the simple way to create a map: Project > Save an Image which creates a png file of the map window. That is what I always use (my use of QGIS is more as an interactive analysis tool).

I tried to use the Print Composer for Hudson, and could never get the Bing Street Maps to line up with the Census Shapefiles. If you use the Census Shapefiles for street features, each street fragment (a block line) is a feature. So you end up with a lot of labeled streets. I merged street features, but it was a tedious process.

I’ll put that in.

My main problem with printing directly from QGIS is that it’s easy to unintentionally move the map in the map window, or change the zoom level, etc. This makes it more difficult to make gifs, where you need the same view of all pictures or the map will jump around. Yes, you can always just write down the extents and zoom level so that you can manually reset, save the project as a different name and reload when you unintentionally move the map or show the use previous view magnifying glass to get back to the old view (if you can remember which it was, anyway), but why bother when the Print Composer doesn’t change extents unless you force it to?

It’s also good that you can easily add a background and legend using the print composer. Otherwise, I’d have to do that manually for every map using Snagit or Paint, anyway, so I actually save time when I make multiple maps by using the print composer. The legend funnel feature is also excellent, as it gets rid of colors in the scale that are not used that portion of the map, making the legend shorter.

But again, YMMV.

Quote
You must be logged in to read this quote.

Yeah, we should probably add that in another tutorial. Lines can get annoying.

Is there a simplified state map somewhere on Census’ website, like there is for counties? I generally only have the tigerline files on my computer.

Quote
You must be logged in to read this quote.

I probably didn’t want to include the percentage change in my map, either - but I wanted to show how to make a multiple-line label (which even I didn’t know how to do until I reread your Hudson GIS tutorial). The multiple-line label is probably what is causing Fairbanks and Ketchikan to disappear when we have the label collision avoidance on. Bristol Bay is going to always be off unless you use a method to assign it or Lake and Peninsula Borough separately. It’s too small and too close to the centroid of Lake & Peninsula Borough.

I’ll add a tutorial on rules-based labeling eventually.
Logged
cinyc
Atlas Icon
*****
Posts: 12,721


Show only this user's posts in this thread
« Reply #26 on: November 04, 2017, 12:20:07 AM »
« Edited: November 07, 2017, 12:42:17 AM by cinyc »

Calculating Wincodes

In this tutorial we will show how to create maps of precinct-level election results. Each precinct will be colored to show the winning party, and their voting strength. For example, using the Atlas color scheme, an inner-city precinct might be shown in a deep red (signifying a big Democratic victory), while a suburban precinct won by a plurality in a three-way race might be shown as light pink or light blue.

For this tutorial we need a precinct shapefile and the election data in a tabular form in a spreadsheet. The precinct shapefile for Alaska is here.

In Part 1 of this tutorial we will prepare the election data in a spreadsheet. Since Part 1 is not about QGIS per se, you may skip to Part 2 and begin with the spreadsheet prepared in Part 1.

Calculating Wincodes - Part 1: Creating Tabular Election Data for Use With QGIS.

Our xlsx file will have one row per precinct. Columns will have the precinct name or identification, as well as the votes cast for each candidate in that precinct. We begin with the election results from the Alaska Division of Elections, focusing on the 2014 general election precinct election results. Unfortunately, the Alaska Division of Elections' results aren't in that format and require processing.

(1) Download the 2014 TEXT by Precinct results from the state of Alaska's elections result website by RIGHT clicking on the link to it on Alaska's 2014 results page (i.e. not here) and selecting Save Link As. Otherwise, this will likely open up in your browser. You can save it as a .txt or .csv file. (Yes, Alaska's format is sub-optimal, but at least Alaska offers a TXT file for those years instead of just a PDF that requires even more processing to get into something Excel readable).

(2) Open up the precinct-results.txt. To do this if Excel is not your default application to open .txt files, open up Excel, then click on File-->Open-->Browse. A dialog window should open up. Make sure to change All Excel Files in the box next to Filename to All Files or All Text Files, if that's not selected already. Otherwise, you won't find your file.

(3) The Text Import Wizard should open up.
(3a) Select the radio button (circle) next to Delimited. Hit Next.
(3b) On the next screen, click the box next to Comma. Unclick any other boxes that might be checked. Hit next.
(3c) On the final screen, General format is okay for this exercise. Hit Finish.

Your data should now be imported into Excel.

(4) Rename this sheet Data (Double Click on the tab at the bottom of the screen, delete the text there and type "Data")

Next, we'll copy the precinct numbers to a new sheet and delete duplicates so that we end up with a list of unique precincts.
(5a) Copy all of row A by clicking on the column header and copying the text however you do that (I usually hit CTRL-C).
(5b) Click on the + icon to create a new sheet.
(5c) Paste the contents of Column A into Cell A1 on new Sheet1. This can be done by hitting CTRL-V.
(5d) On the top ribbon, select Data-->Remove Duplicates. Hit OK. Duplicates have been removed. See the image below for the location of "Data" and "Remove Duplicates", next to the red circle 5.
(5e) Insert a new row 1. This can be done by clicking on the row number (1), then RIGHT clicking and selecting Insert.
(5f) Type "Precinct Name" in cell A1.



Note: In Alaska, early, absentee, and questioned votes are not attributed to the election precinct of the voter, but rather to the legislative district. These votes represented about 30% of the total vote in 2014. We will not be able to display these results on our precinct-level maps, but we will retain them for use in future analysis (or tutorials).

(6) We will need to separate the precinct number from precinct name to match the data in the Alaska precinct shapefile. Luckily, all precinct IDs are 6 characters.
(6a) In Cell B2, Type "Precinct ID"
(6b) In cell B2, Type "=LEFT(", scroll over to Cell A2, and then type ",6" - or just type =LEFT(A2,6)
(6c) Copy that formula to the bottom of the data.

There are a number of ways to do this. One way is just to hit CTRL-C, scroll down until you hit the end of the data in Column A, and hit CTRL-V. This might work well when you only have a few hundred cells to paste data into, but not so much when there are thousands or tens of thousands of cells.

One Excel shortcut is to hit CTRL-End to get to the end of your worksheet, click on B562 (i.e. last row) and then SHIFT-CTRL-UP ARROW to select the entire column followed by CTRL-D. If you know the last row number, you can also do a GO TO (type CTRL-G, then B2:B562) and hit CTRL-D. Another way to do this is Hit CTRL-C to copy cell B2. Go to neighboring Column A and Press CTRL-Down Arrow enough times to get to the bottom of the entries in that column with text. Once at the true bottom, use the Right Arrow to scroll back to the cell on that line in blank Column B. The first box on the Function toolbar should now say B562. In that box (below the red circle 6 on the image above), type "B2:" before the B562 and hit enter. That should create a box from B2 to B562. Hit CTRL-V.

Next, we'll calculate the number of votes each candidate in the 2014 Senate, House, and Governor's race received in a format that can be more easily joined with the shapefiles:

(7a) Type the following headers in Cells C1-P1

Begich, Mark    Fish, Mark S.    Gianoutsos, Ted    Sullivan, Dan    Write-in 70    Dunbar, Forrest    McDermott, Jim C.    Young, Don    Write-in 50    Clift/Lee    Myers/Rensel    Parnell/Sullivan    Walker/Mallott    Write-in

I got these by cutting and Pasting Special TRANSPOSE (Right Click T or hit the Clipboard with round arrow) the the names of the Senate, House and Gubernatorial Candidates from the prior spreadsheet. You can, too. Exact names make it easier to do the =sumifs function described below.

(7b) Type the following formula into Cell C2:
=SUMIFS(Data!$F$1:$F$56184,Data!$C$1:$C$56184,C$1,Data!$A$1:$A$56184,$A2,Data!$B$1:$B$56184,Data!$B$3)

Formula explanation:
-Data! Column F is the number of votes that need to be summed. That goes first in a =SUMIFS function, but last in a simple =SUMIF function.
-Data! Column C is the Candidate Name. It gets compared to the candidate name in the header row of this sheet.
-Data! Column A is the Precinct Name. If gets compared to the Precinct Name in Column A of this sheet.
-Data! Column B is the office name. It gets compared to the office name the first time it appears on Data! B for that race. We're going to have to change the last variable when we cut and paste it for the House and Governor's Race. I'd go with two header rows to include this variable, but that might mess up the join in QGIS. And typing the name isn't necessary.

(7c) Copy this formula to Cells D2-H2. (Hit CTRL-R in D2-H2 or CTRL-C in Cell C2, followed by CTRL-V in D2-H2)
(7d) Change "Data!$B$3" to "Data!$B$12" in H2.
(7e) Copy Cell H2 to Cells I2-L2.
(7f) Change "Data!$B$12" to "Data!$B$20" in L2.
(7g) Copy Cell L2 to Cells M2-P2.
(7f) Copy cells B2-O2 to row 562.

After a few seconds, the data should update. At this time, it's probably prudent to make sure the precinct results matched the actual results.

(8a) Type "Total" in Cell A564.
(8b) Type =SUM(B2:B564) in Cell C564.
(8c) Copy the formula in C564 to Cells C564-P564.
(8d) Check the totals against the official total election results on Alaska's website. That's available here.

This step matters. When I attempted this the first time, I noticed the write-in totals were off for some races without adding the race name variable to the =SUMIFS function.

Assuming everything checks out correctly, we're ready to compute Wincodes. Before you do that:

(9a) In Cell A1, Select All (Hit CTRL-A).
(9b) Copy that data.
(9c) Open a new blank spreadsheet.
(9d) Paste VALUES in Cell A1 of the new blank spreadsheet.
(9e) Save this spreadsheet as "AK2014 Tutorial" in Excel format.

Or just click on the link to my AK2014 Tutorial.xlsx in the next post.
Logged
cinyc
Atlas Icon
*****
Posts: 12,721


Show only this user's posts in this thread
« Reply #27 on: November 04, 2017, 01:11:29 AM »
« Edited: July 16, 2020, 05:19:41 PM by cinyc »

Calculating Wincodes - Part 2: Calculating Wincodes in Excel

If you completed Part 1 on your own, you can use that file. Otherwise, you'll need to download the following Excel file for this part of the exercise:

AK2014 Tutorial.xlsx

The spreadsheet contains precinct-level results for the Alaska's Senate, House and Gubernatorial Race in the 2014 General Election. We need to manipulate the data to create Wincodes:

(1) Add the following titles in Cells Q1-V1:
Senate Total   House Total   Governor Total   Sen Wincode   House Wincode   Gov Wincode
(2) Type the following =SUM functions to add up the votes in each race:
(2a) Cell Q2:
=SUM(C2:G2) | Sum of Votes in Senate Race
(2b) Cell R2:
=SUM(H2:K2) | Sum of Votes in House Race
(2c) Cell S2:
=SUM(L2:P2) | Sum of Votes in Governor's Race

(3) Next, we'll calculate the Wincode for each race. A Wincode is basically the winner's percentage, increased by the integer necessary to color the precinct the proper shade. For General Elections on Atlas, Republicans are usually blue, Democrats red, Libertarians yellow and Independents or Green party candidates green. In my 7 color style (which is actually 8 colors), for example, blue is in the 0s, green is in the 1s and red is in the 2s. Thus, the general formulas are as follows:

Republican     | PCT
Ind/Green      | PCT +1
Democrat       | PCT +2
Libertarian     | PCT +3
Others           | PCT +4,5,6 or 7.
Tie                | 9
No Data         | 10
Error             | -1 or 11, depending on the color scheme

Ties are generally represented by light gray, no data by dark gray, and errors by white.

Thus, you need to know which candidates are Republicans, which are Democrats, and which are others when creating your Wincode formula. Depending on how the elections officials placed the candidates in the results, they are unique to each race.

I calculate Wincodes using a nested IF function. Here, we haven't precalculated the percentage for each candidate. We can do that - but need not here. It's a simple formula - ={Cell of candidate votes}/{Cell of total votes}.

Here are the formulas for the wincodes. Type (or cut and paste) the following formulas:

(3a) In Cell T2 (Senate Wincode):
=IF(Q2=0,10,if(MAX(C2:G2)=LARGE(C2:G2,2),9,IF(F2=MAX(C2:G2),F2/Q2,IF(C2=MAX(C2:G2),C2/Q2+2,IF(D2=MAX(C2:G2),D2/Q2+3,IF(E2=MAX(C2:G2),E2/Q2+1,11))))))

Explanation of the formula:
- Since we're not worried about precincts not yet reporting in the final results (in which case, the first IF would be "IF(Q2=0,10" where Q2 is the total votes cast, checking for ties comes first. MAX(C2:G2)=LARGE(C2:G2,2),9 compares the largest number of votes received in the Senate race against the second highest number of votes received. If they are equal, it's a tie, and QGIS will color the geometry as light gray.
- Next, we check to see if the Republican candidate, Sullivan, received the most votes. If that's true, the wincode is just the Republican's percent, F2/Q2, which would give us a shade of blue in QGIS.
- Then, we check to see if the Democratic candidate, Begich, received the most votes. If that's true, the wincode is the Democrat's percent plus 2, C2/Q2+2, which would give us a shade of red in QGIS.
- Fourth, we check if the Libertarian candidate received the most votes. If that's true, the wincode is the Libertarian's percent plus 3, D2/Q2+3, which would give us a shade of yellow in QGIS.
- Fifth, we check if the Non-affiliated candidate received the most votes. If that's true, the wincode is that candidate's percent plus 1, E2/Q2+1, which would give us a shade of green in QGIS.
- Last, if none of these are true, the wincode is 11, which would give us a blank white precinct, telling us something's wrong - either Write-ins won or we screwed up.

(3b) In Cell U2 (House Wincode):
=IF(MAX(H2:K2)=LARGE(H2:K2,2),9,IF(J2=MAX(H2:K2),J2/R2,IF(H2=MAX(H2:K2),H2/R2+2,IF(I2=MAX(H2:K2),I2/R2+3,11))))

This formula is shorter because there were fewer candidates in the race.

(3c) So what should be the formula for the Governor's Wincode? Try to calculate this on your own. Here are the partisan affiliations of the candidates:
Clift/Lee           | Libertarian
Myers/Rensel    | Constitution
Parnell/Sullivan | Republican   
Walker/Mallott  | Main Independent

Note: There was no Democrat in the race.

The formula I used is below in white text. Highlight it if you want to know the answer, and cut and paste it to Cell V2 your spreadsheet (you might need to change the font color in Excel for that Cell to black if you cut and paste the formula):

=IF(MAX(L2:P2)=LARGE(L2:P2,2),9,IF(N2=MAX(L2:P2),N2/S2,IF(O2=MAX(L2:P2),O2/S2+1,IF(L2=MAX(L2:P2),L2/S2+3,IF(M2=MAX(L2:P2),M2/S2+4,11)))))

(4) Copy and paste cells P2-U2 to rows P3 to U443.
(5) Save your spreadsheet.

Now that you've calculated Wincodes, we're ready to move on to the next step: Drawing a multiple layer precinct election map in QGIS.
Logged
jimrtex
Atlas Icon
*****
Posts: 11,828
Marshall Islands


Show only this user's posts in this thread
« Reply #28 on: November 04, 2017, 04:24:52 AM »


One thing you might show, particularly for the Contiguous US, is how to turn the county boundaries off. That is an awful lot of black which overwhelms the colors. If you do that, then you might want to bring a States layer in, and show how to display layers that cover the same area.

Yeah, we should probably add that in another tutorial. Lines can get annoying.

Is there a simplified state map somewhere on Census’ website, like there is for counties? I generally only have the tigerline files on my computer.
From the home Census Bureau webpage, select the Geography tab at the top, then Maps&Data. Select  TIGER Products, and then Cartographic Boundary Shapefiles. Incidentally, the Census Bureau uses:

TIGER/Line Shapfiles

Tigerlines may be a neologism.

There are quite a bit more maps than were available in the past. A weakness of using separate cartographic files is that they have been simplified independently, and may not match up perfectly (probably close enough for visual purposes. It could be better to generate a state file from the county file in QGIS. This is pretty easy to do for the shapes themselves, but the only useful thing left in the DBF file would the State ID. You could add any data, such as state names and populations easily.
Logged
cinyc
Atlas Icon
*****
Posts: 12,721


Show only this user's posts in this thread
« Reply #29 on: November 04, 2017, 11:42:35 AM »
« Edited: November 07, 2017, 12:48:11 AM by cinyc »

Calculating Wincodes Part 3: Drawing a multiple layer precinct election map in QGIS.

Now that we have our 2014 Alaska election data in order, it's time to make maps with it. For this exercise, you'll need the following additional items:

Shapefiles:
(1) Alaska's Precinct Shapefile
(2) My shapefile of Alaska's Coastal Water Features, AlaskaBays.zip

These two items should be unzipped and put somewhere on your computer where you can find them. I have a separate folder for each state's GIS files, but I do a lot of mapping. Putting all your GIS files in one directory should suffice.

QGIS Style Files:
(1) Atlas 7Color Scale
(2) Water
(3) Boundary Yellow

And if you haven't completed steps 1 and 2, you'll need the following Excel File:
AK2014 Tutorial Full

Let's get started.

(1) Open QGIS.

(2) If you've saved your project from the last lesson, open it. Otherwise:
(2a) Open cb_2016_us_county_5m.shp
(2b) Open gpr_000b11a_e.shp
(2c) In the layer panel, rename cb_2016_us_county_5m "County"
Note: This doesn't change the underlying file name, just the name in the Layer Panel. The layer name is used when the print composer auto-makes a legend - though you could separately rename the shapefile name only in the print composer, if you wish.
(2d) Rename gpr_000b11a_e "Canada".
(2e) Change the projection to Alaska_Albers_Equal_Area_Conic
(2f) Select the Zoom tool from the toolbar and draw a box around Alaska. You needn't include all of the Aleutians.
If you don't know how to do any of these steps or need the shapefiles, read the prior lessons.

(3) Open 2013-SW-Precinct-Proc-Plan.shp, the Alaska precinct shapefile. Rename it "Precinct".

(4) Open AlaskaBays.zip. Rename it "Water".

Your screen should look something like this.



Let's start coloring the various layers from bottom up using styles:

(5a) Select County in the Layer Panel.
(5b) Right Click and select Properties.
(5c) Select Style from the menu on the left of the popup dialog if it isn't already selected.
(5d) Click on Style on the bottom of the dialog. Select "Load Style" and Load Boundary Yellow.qml.
(5e) Hit Okay.

Because County is on the bottom in the Layer Panel, the only change you should see is that the county labels are gone. In QGIS, layers at the top of the layer panel are the top layer on the map. In order to see the County lines:

(5f) Select and drag County to the top of the layer panel.

Your map should now look something like this:


We need to join the data to the Precinct shapefile before we can color it, so:

(6a) Add 2014AK Tutorial Full.xlsx (or whatever you called your file from Step 2 of the previous tutorial) as a Vector File. (Hit the V icon on the left of the screen. If your Excel file doesn't show up in the file browser, make sure All Files or MS Office Open XML Spreadsheet is selected in the drop-down menu next to filename.)
(6b) Select Precinct in the Layer Panel.
(6c) Right click on Precinct and select Properties.
(6d) Select Joins from the menu on the left of the popup dialog.
(6e) Hit the plus button on the bottom of the dialog to add a join.
(6f) Make sure the following are selected in the popup's pull-down menus:
Join Layer: AK2014 Tutorial Full Sheet1 None (or whatever the just-added data layer is called on your computer)
Join field: Precinct ID
Target field: District
(6g) Check the box next to Custom field name prefix and delete the text in it.
(6h) Hit OK.
(6i) Check to see that the Join worked by clicking on the Connecticut Avenue Monopoly Card icon and making sure there are numbers in all of the columns. Assuming the join was successful:
(6j) Select Precinct in the Layer Panel.
(6k) Right click on Precinct and select Properties.
(6l) Select Styles from the menu on the left of the popup dialog.
(6m) Click on Style on the bottom of the dialog. Select "Load Style" and Load 7ColorScale.qml.
(6n) Click on the drop-down menu next to column. Change WINCODE to Sen Wincode.
(6o) Hit OK.

Your map should now look something like this:



Finally, we need to change the water color, using the Water style. Try doing this on your own. If you're lost, all you need to do is the following:

(7a) Select Water in the Layer Panel.
(7b) Right click on Precinct and select Properties.
(7c) Select Styles from the menu on the left of the popup dialog.
(7d) Click on Style on the bottom of the dialog. Select "Load Style" and Load Water.qml.

Your map should look something like this:



Congratulations! You've made a precinct map of the 2014 Alaska Senate Election! Save your project (or save as under a different name, if you want to save each lesson separately).

If you want to print a simple image of your map, you can select Project—> Save an Image which creates a png file of the map window. Otherwise, please continue to the next part of this lesson, where I will show how easy and quick it is to make multiple professional-looking maps using QGIS' print composer.
Logged
cinyc
Atlas Icon
*****
Posts: 12,721


Show only this user's posts in this thread
« Reply #30 on: November 04, 2017, 12:11:34 PM »

Calculating Wincodes Part 4 - Using the QGIS Print Composer to make multiple maps

(1) Open QGIS and load the project from Step 3.

(2) Open the Print Composer you created in the Using the QGIS Print Composer Lesson. Assuming you named your last print composer Alaska, select Project-->Print Composers-->Alaska.

That will open up your print composer in a new window. The map may or may not have auto updated. To update the map:

(3) Highlight map0 in the Items Pane, select the Item Properties Tab below (if it's not open already) and hit "Update preview".

We need to change the title of the map:

(4a) Select the item that says "Relative Population Chang...." or "QGIS" in the item pane.
(4b) Under Main Properties, delete the text in the box and type "2014 U.S. Senate Results by Precinct"

The legend should have auto-updated. If not:

(5a) Select Legend in the item pane.
(5b) Under Legend Items, toggle the Auto update box on, off and on again.

(6) Print your map by hitting the mountain image icon on the top of the screen.

That's it.

Now, we can easily change the variable mapped.

(7) Open your main QGIS screen.

(8a) Select Precinct in the Layer Panel.
(8b) Right click on Precinct and select Properties.
(8c) Select Styles from the menu on the left of the popup dialog.
(8d) Click on the drop-down menu next to column. Change Senate Wincode to House Wincode.
(8e) Hit okay.

The colors on your map should change. In general, it should look more blue because Republican Don Young won more precincts in the House race than Dan Sullivan in the Senate Race.

(9) Open the Print Manager window.

(10) Highlight map0 in the Items Pane, select the Item Properties Tab below (if it's not open already) and hit "Update preview".

We need to change the title of the map:

(11a) Select the item that says "2014 U.S. Senate Results...." or "QGIS" in the item pane.
(11b) Under Main Properties, delete the text in the box and type "2014 U.S. House Results by Precinct"

The legend should have auto-updated. If not:

(12a) Select Legend in the item pane.
(12b) Under Legend Items, toggle the Auto update box on, off and on again.

(13) Print your map by hitting the mountain image icon on the top of the screen.

Try to do the same for the Governor's race on your own. The relevant variable is Gov Wincode. Your map should be green and blue since there was no Democrat in the race.

That's it! It's pretty simple, right? As always, remember to save your project.
Logged
heatcharger
Junior Chimp
*****
Posts: 5,233
Sweden


Political Matrix
E: -1.04, S: -0.24

Show only this user's posts in this thread
« Reply #31 on: November 04, 2017, 12:41:34 PM »

Damn, this is pretty awesome of you to do. I've always wanted to learn how to use QGIS.
Logged
cinyc
Atlas Icon
*****
Posts: 12,721


Show only this user's posts in this thread
« Reply #32 on: November 04, 2017, 03:27:06 PM »

Damn, this is pretty awesome of you to do. I've always wanted to learn how to use QGIS.

Thanks! Also see jimrtex's excellent Hudson QGIS thread from last year to learn some more advanced editing features of QGIS.
Logged
cinyc
Atlas Icon
*****
Posts: 12,721


Show only this user's posts in this thread
« Reply #33 on: November 04, 2017, 04:12:47 PM »
« Edited: November 04, 2017, 04:17:30 PM by cinyc »

Calculating Wincodes Part 5 - Making an Inset Map in the Print Composer

The precinct map of Alaska is nice and all, but it really emphasizes rural areas at the expense of populated cities, like Anchorage. We can use the print manager to draw an Anchorage inset map. It's easier than you think.

(1) Open the Alaska print composer (if you're continuing the lesson immediately after completing Part 4, just select the print composer in Windows, otherwise open your project in QGIS and select Project-->Print Composers-->Alaska.

(2) In the print composer, select Composer-->Duplicate Composer from the drop down menu. You can also click on the icon that looks like a white piece of paper on top of a grey one with an orange star. (It's probably the 2nd icon to the right after the save diskette.) See the image below if you can't find it:



(3) Give your new composer a name: Alaska with ANC Inset.

A new print composer should pop up.

(4) Go to your main QGIS screen, select the plus magnifier icon, and draw a box around Anchorage. If you don't know where Anchorage is, it's in South Central Alaska. Your zoom should look something like this:



(5a) Open up the print composer. Create a new map by hitting the new map icon. (If you don't remember which that is, it's marked as 5 in the image under step 2.)
(5b) Draw a rough square somewhere where there's free space on the map. I drew mine in the crook between Kodiak Island and the map scale.

A new map, Map1 should pop up. Map0 and Map1 are confusing names, so:
(6a) Double click on Map0 in the Item pane. Change the name to Alaska.
(6b) Double click on Map1 in the Item pane. Change the name to ANC Inset.

Now, we're ready to make the inset:
(7a) Select the Item properties tab underneath the Item pane for ANC Inset if it isn't already.
(7b) Under extents, hit the Set map to canvas extent button

The inset is probably not as zoomed in on Anchorage as it could be:

(7c) Play around with the coordinates under extents until Anchorage fills the inset. I used the following coordinates:
X Min: 200000
Y Min: 1200000
X Max: 300000
Y Max: 1290000

(7d) Move the inset to make sure it's not obscuring Kodiak Island or other parts of the map, including the scale. Make sure it is aligned with the bottom of the page (a red line should come up when it is).

(7e) If you need to make the inset smaller or larger, click on the upper left corner and move diagonally.

(7f)  Open the Position and size menu on the Items tab. Click the bottom center square next to Reference Point.

Next, we'll add a title to the inset:

(8a) Click on the add text icon. It's the piece of paper with a T.
(8b) Draw a rectangle above your inset.
(8c) Type "Anchorage" or "Anchorage Inset" in the box under Main properties.
(8d) Change the font as you deem fit. I used MS Shell Dig 2, Bold, 12 pt.
(8e) Open the Position and size menu on the Items tab. Click the bottom center square next to Reference Point.


(9) Add a scale bar, if you want, by clicking on the Anchorage Inset map, then the scale icon, drawing a rectangle, and following the instructions in the basic print composer tutorial. It's probably unnecessary.

No new legend is necessary, as Anchorage is included on the main map, and the color scale for precincts in the Anchorage inset are already included.

(10) Export your new map as an image by hitting the Export image icon.

That's it! You've made a map with an inset! It should look something like this:



Like the main map, the inset map can be easily updated when you change variables. Just remember to hit the Update preview button on BOTH maps - but everything should auto-update when you save as a png file, anyway.

Remember to save your project!
Logged
jimrtex
Atlas Icon
*****
Posts: 11,828
Marshall Islands


Show only this user's posts in this thread
« Reply #34 on: November 04, 2017, 06:16:44 PM »

Calculating Wincodes - Part 1: Manipulating Certain Types of Raw Data

Now that you know how to make a basic map, I will now move on to a lesson on how to calculate WINCODES to be used with the Atlas 8 Color Code color scheme. Part 1 is more a lesson about how to use Excel to make sense of poorly formatted election results than actually calculating Wincodes. If you already know how to do this, feel free to skip to part 2.
What are WINCODES? It would help me know what you are trying to make your xlsx file look like, and what you are ultimately try to produce. It would help to know what your shapefiles contain since we are trying to match them.

Particularly for exemplary purposes, I would focus on a single race. If you want to compare different results, you can always do multiple joins. I would have kept the Absentee, Questionable, and Early Voting separate by legislative district. Since Alaska does not attribute these votes back to election precinct, and they represent about 1/3 of votes, I would do an analysis of how these results varied by legislative district (e.g. share of in person votes, whether different types of votes were politically different. It might be possible to adjust the precinct in person votes. At minimum, you could aggregate results by legislative district.

Quote
You must be logged in to read this quote.
If you save the precinct results as a .csv file, you can open them directly in Excel. Note that "circle" is called a "radio button"

Quote
You must be logged in to read this quote.
Possibly premature to extract ID's, since this is where we merged all of the absentee, questionable, and early votes into a pseudo precinct called "Distri". If we kept them separate, we could create precinct IDs like 40-ABST, 40-QUES, 40-EARL which could then be aggregated into totals for the legislative district. It doesn't matter if not all data can be joined. Note, there are ways to check when there are shapefiles, but with no data to join with them.

If I had done it your way, I would type: "=LEFT(", then click on cell A1, and then typed ",6)" at the point where you select A1, press function key F4 and you can cycle through various absolute and relative expressions. $A1 might be a little bit better if you were going to also fill in columns to the right.

I would then do a GOTO  (G1:G56184) and pressed CTRL-D to fill down. CTRL-R fills right and you can fill up or left from the Fill command.

You could also do a CTRL-End to get to the end of your worksheet, click on G56184 (i.e. last row) and then SHIFT-CTRL-UPARROW to select the entire column followed by CTRL-D.

Quote
You must be logged in to read this quote.
I couldn't find Select Data-->Remove Duplicates.  Instead I clicked on the light bulb and typed "Remove Duplicates" and a menu popped up.

Quote
You must be logged in to read this quote.
I would have entered in B2

=SUMIFS(Data!$F$1:$F$56184,Data!$C$1:$C$56184,Sheet1!B$1,Data!$G$1:$G$56184,Sheet1!$A2)

And then used CTRL-R and CTRL-D to copy across and down. I had not understood why you also select on the Office because of the weird way they did write-ins. When I went back to correct for the write-ins I added the Office criteria for only the write-ins.

Quote
You must be logged in to read this quote.
This is critical. You'll never quite figure out the format that was used (Alaska included results for Senate District 5, rather than Senate District S). Not only did I not have the write-ins, I didn't understand that the "Distri" results were not district totals and repeated.

I've never found a simple way to do partial transposes. You might write a script, but by the time you got it working, you could have used a more brute force method. If every state used the same format, it might work to develop a more automated way.

Amother approach would be sort by Office, Candidate, and Precinct. You could then just copy the column for each candidate. If I did this, I would double check that the precinct order was the same for each column.

Quote
You must be logged in to read this quote.

This is very important step. We want the data in this format, as if the state had provided it this way. You might want to start this tutorial showing the desired end result. The rest is not necessarily showing how to do it, but to suggest a way short of timing in all the results by hand, which would discourage the attempt. Someone reading the tutorial could then skip to the next message.
Logged
cinyc
Atlas Icon
*****
Posts: 12,721


Show only this user's posts in this thread
« Reply #35 on: November 04, 2017, 10:52:53 PM »

What are WINCODES? It would help me know what you are trying to make your xlsx file look like, and what you are ultimately try to produce. It would help to know what your shapefiles contain since we are trying to match them.

Particularly for exemplary purposes, I would focus on a single race. If you want to compare different results, you can always do multiple joins. I would have kept the Absentee, Questionable, and Early Voting separate by legislative district. Since Alaska does not attribute these votes back to election precinct, and they represent about 1/3 of votes, I would do an analysis of how these results varied by legislative district (e.g. share of in person votes, whether different types of votes were politically different. It might be possible to adjust the precinct in person votes. At minimum, you could aggregate results by legislative district.

I'll add a sentence describing WINCODES. It's already explained more in part two - In order to match the relevant QGIS winner color, we need to add an integer to the percentage of all those who won a precinct except Republicans (since the R color is in the 0-1 range). For example, a D winner has to have 2 added to their percentage if they win a precinct to generate a red color in QGIS using the 7 Color scheme.

I checked the shapefile before determining whether I had to truncate the precinct code. A link to the relevant shapefile is in Part 3 of the discussion. Unfortunately, the full precinct names from the Alaska Precinct shapefile are in terrible shape - some are capitalized, some have extraneous spaces, etc. I couldn't do a join on that column (NAME) without getting NULLs.

I wasn't planning on using the data to sum up the results by House District. You're right - that can be another lesson. I suppose we can do the remove duplicates first and then create a Precinct column in Sheet1 instead of doing it on the Data sheet. I'll rewrite the write-up to do that.

I like to have all of one year's data in the same spreadsheet without doing multiple joins. One of the purposes of the exercise was to show how to change variables and generate a new map in the print composer in Part 4.

Quote
You must be logged in to read this quote.
If you save the precinct results as a .csv file, you can open them directly in Excel. Note that "circle" is called a "radio button"[/quote]

OK
Quote
You must be logged in to read this quote.
Possibly premature to extract ID's, since this is where we merged all of the absentee, questionable, and early votes into a pseudo precinct called "Distri". If we kept them separate, we could create precinct IDs like 40-ABST, 40-QUES, 40-EARL which could then be aggregated into totals for the legislative district. It doesn't matter if not all data can be joined. Note, there are ways to check when there are shapefiles, but with no data to join with them.

If I had done it your way, I would type: "=LEFT(", then click on cell A1, and then typed ",6)" at the point where you select A1, press function key F4 and you can cycle through various absolute and relative expressions. $A1 might be a little bit better if you were going to also fill in columns to the right.

I would then do a GOTO  (G1:G56184) and pressed CTRL-D to fill down. CTRL-R fills right and you can fill up or left from the Fill command.

You could also do a CTRL-End to get to the end of your worksheet, click on G56184 (i.e. last row) and then SHIFT-CTRL-UPARROW to select the entire column followed by CTRL-D.[/quote]
What's a GOTO?

That is how I generated the formula - moused over to A1, then hit 6. I wasn't planning on adding any additional columns, so I didn't see the need to cycle through the relative values (in fact, I didn't even know <F4> did that - I usually just manually type the dollar signs.

CTRL-End, Shift-Ctrl-Up Arrow, CTRL-D seems to work.
Quote
You must be logged in to read this quote.
I couldn't find Select Data-->Remove Duplicates.  Instead I clicked on the light bulb and typed "Remove Duplicates" and a menu popped up.[/quote]

I knew I should have put a few pictures in this discussion. I also miscapitalized "Select". I'm using the newest version of Excel. It has a Data tab on the top ribbon. If you click on that, there's a Remove Duplicates icon.  I'll add a picture to explain.

Quote
You must be logged in to read this quote.
I would have entered in B2

=SUMIFS(Data!$F$1:$F$56184,Data!$C$1:$C$56184,Sheet1!B$1,Data!$G$1:$G$56184,Sheet1!$A2)

And then used CTRL-R and CTRL-D to copy across and down. I had not understood why you also select on the Office because of the weird way they did write-ins. When I went back to correct for the write-ins I added the Office criteria for only the write-ins.[/quote]

You like CTRL-R and CTRL-D. I prefer CRTL-C and CTRL-V. I suppose CTRL-R/CTRL-D saves a keystroke. I'll add alternative keystroke instructions.

Quote
You must be logged in to read this quote.
This is critical. You'll never quite figure out the format that was used (Alaska included results for Senate District 5, rather than Senate District S). Not only did I not have the write-ins, I didn't understand that the "Distri" results were not district totals and repeated.

I've never found a simple way to do partial transposes. You might write a script, but by the time you got it working, you could have used a more brute force method. If every state used the same format, it might work to develop a more automated way.

Amother approach would be sort by Office, Candidate, and Precinct. You could then just copy the column for each candidate. If I did this, I would double check that the precinct order was the same for each column.

Quote
You must be logged in to read this quote.

This is very important step. We want the data in this format, as if the state had provided it this way. You might want to start this tutorial showing the desired end result. The rest is not necessarily showing how to do it, but to suggest a way short of timing in all the results by hand, which would discourage the attempt. Someone reading the tutorial could then skip to the next message.
[/quote]
OK
Logged
jimrtex
Atlas Icon
*****
Posts: 11,828
Marshall Islands


Show only this user's posts in this thread
« Reply #36 on: November 05, 2017, 12:00:51 AM »

Calculating Wincodes - Part 2: Calculating Wincodes in Excel

Republican     | PCT
Ind/Green      | PCT +1
Democrat       | PCT +2
Libertarian     | PCT +3
Others           | PCT +4,5,6 or 7.
Tie                | 9
No Data         | 10
Error             | 11

Ties are generally represented by light gray, no data by dark gray, and errors by white.

Thus, you need to know which candidates are Republicans, which are Democrats, and which are others when creating your Wincode formula. Depending on how the elections officials placed the candidates in the results, they are unique to each race.

I calculate Wincodes using a nested IF function. Here, we haven't precalculated the percentage for each candidate. We can do that - but need not here. It's a simple formula - ={Cell of candidate votes}/{Cell of total votes}.

Here are the formulas for the wincodes. Type (or cut and paste) the following formulas:

(3a) In Cell S2 (Senate Wincode):
=IF(MAX(B2:F2)=LARGE(B2:F2,2),9,IF(E2=MAX(B2:F2),E2/P2,IF(B2=MAX(B2:F2),B2/P2+2,IF(C2=MAX(B2:F2),C2/P2+3,IF(D2=MAX(B2:F2),D2/P2+1,11)))))

Explanation of the formula:
- Since we're not worried about precincts not yet reporting in the final results (in which case, the first IF would be "IF(P2=0,10" where P2 is the total votes cast, checking for ties comes first. MAX(B2:F2)=LARGE(B2:F2,2),9 compares the largest number of votes received in the Senate race against the second highest number of votes received. If they are equal, it's a tie, and QGIS will color the geometry as light gray.
- Next, we check to see if the Republican candidate, Sullivan, received the most votes. If that's true, the wincode is just the Republican's percent, E2/P2, which would give us a shade of blue in QGIS.
- Then, we check to see if the Democratic candidate, Begich, received the most votes. If that's true, the wincode is the Democrat's percent plus 2, B2/P2+2, which would give us a shade of red in QGIS.
- Fourth, we check if the Libertarian candidate received the most votes. If that's true, the wincode is the Libertarian's percent plus 3, C2/P2+3, which would give us a shade of yellow in QGIS.
- Fifth, we check if the Non-affiliated candidate received the most votes. If that's true, the wincode is that candidate's percent plus 1, D2/P2+1, which would give us a shade of green in QGIS.
- Last, if none of these are true, the wincode is 11, which would give us a blank white precinct, telling us something's wrong - either Write-ins won or we screwed up.

I would use a pair of lookup tables:

Begich DEM
Fish     LBT
Gianoutsos IND
Sullivan  REP
Write-in IND

REP 0
IND 1
DEM 2
LBT 3

I would use several columns.

The first would check for a tie, the same way you did.
The next would find the relative column of the winning candidate, where columns C:G have the votes of the candidates.

    =MATCH(MAX(C2:G2),C2:G2,0)

Name of the winning candidate.

    =INDEX($C$1:$G$1,1,M2)

Party of the winning candidate

    =VLOOKUP(O2,$O$8:$P$12,2)

Wincode base of the party:

    =VLOOKUP(P2,$O$14:$P$17,2)

Or you could add two rows, one for the party of the candidate, and the  other for the wincode base of the winning candidate. Since you know the column of the winning candidate for a precinct, calculating the percentage and the wincode base is trivial.
Logged
jimrtex
Atlas Icon
*****
Posts: 11,828
Marshall Islands


Show only this user's posts in this thread
« Reply #37 on: November 05, 2017, 12:05:41 AM »

Calculating Wincodes Part 3: Drawing a multiple layer precinct election map in QGIS.

(6g) Check the box next to Custom field name prefix and delete the text in it.
I hadn't been doing this. The consequence is that the added field have a prefix, and there is a character limit for field names (I think in dBase). As a result the names of fields get truncated.
Logged
jimrtex
Atlas Icon
*****
Posts: 11,828
Marshall Islands


Show only this user's posts in this thread
« Reply #38 on: November 05, 2017, 09:43:55 PM »

What are WINCODES? It would help me know what you are trying to make your xlsx file look like, and what you are ultimately try to produce. It would help to know what your shapefiles contain since we are trying to match them.

Particularly for exemplary purposes, I would focus on a single race. If you want to compare different results, you can always do multiple joins. I would have kept the Absentee, Questionable, and Early Voting separate by legislative district. Since Alaska does not attribute these votes back to election precinct, and they represent about 1/3 of votes, I would do an analysis of how these results varied by legislative district (e.g. share of in person votes, whether different types of votes were politically different. It might be possible to adjust the precinct in person votes. At minimum, you could aggregate results by legislative district.

I'll add a sentence describing WINCODES. It's already explained more in part two - In order to match the relevant QGIS winner color, we need to add an integer to the percentage of all those who won a precinct except Republicans (since the R color is in the 0-1 range). For example, a D winner has to have 2 added to their percentage if they win a precinct to generate a red color in QGIS using the 7 Color scheme.

I checked the shapefile before determining whether I had to truncate the precinct code. A link to the relevant shapefile is in Part 3 of the discussion. Unfortunately, the full precinct names from the Alaska Precinct shapefile are in terrible shape - some are capitalized, some have extraneous spaces, etc. I couldn't do a join on that column (NAME) without getting NULLs.

I wasn't planning on using the data to sum up the results by House District. You're right - that can be another lesson. I suppose we can do the remove duplicates first and then create a Precinct column in Sheet1 instead of doing it on the Data sheet. I'll rewrite the write-up to do that.
I would start with something like this:

In this tutorial we will show how to create maps of precinct-level election results. Each precinct will be colored to show the winning party, and their voting strength. For example, an inner-city precinct might be shown in a deep red, while a suburban precinct won by a plurality in a three-way race might be shown as light pink or light blue.

For this tutorial we need a precinct shapefile and the election data in a tabular form in a spreadsheet. The precinct shapefile for Alaska is here <link to file>. In Part 1 of this tutorial we will prepare the election data in a spreadsheet. Since Part 1 is not about QGIS per se, you may skip to Part 2 and begin with the spreadsheet prepared in Part 1.

Part 1: Creating Tabular Election Data for Use With QGIS.

Our xlsx file will have one row per precinct. Columns will have the precinct name or identification, as well as the votes cast for each candidate in that precinct. We begin with the election results from the Alaska Division of Elections <link>.

[Description of format and loading of file.]

Note: In Alaska, early, absentee, and questionable votes are not attributed to the election precinct of the voter, but rather to the legislative district. These votes represented about 30% of the total vote in 2014. We will not be able to display these results on our precinct-level maps, but we will retain them for use in future analysis (or tutorials).

<how to generate the base spreadsheet>

I would then split Part 3 into two. One would generate the IDs used for the Join. You could open the precinct shapefile to show the format, and explain how you are generating the ordinary ID. We want to retain the original precinct names as well since they include the polling place names. I used 01-EAR, 01-ABS, and 01-QUE, etc. for the other results.

Somewhere we can explain what happens when there is a mismatch between the join variable.

Quote
You must be logged in to read this quote.
Right Click on Properties, Select Join, Click on the + sign, Select the file to be joined, select the source and target variables, Click on OK. Repeat.

Then generate the maps.

If you had data for the 2016 election, there would be no need or reason to put it in a spreadsheet with the 2014 data.

Quote
You must be logged in to read this quote.
What's a GOTO?

That is how I generated the formula - moused over to A1, then hit 6. I wasn't planning on adding any additional columns, so I didn't see the need to cycle through the relative values (in fact, I didn't even know <F4> did that - I usually just manually type the dollar signs.
[/quote]
Ctrl G is the GO TO command. It is sometime useful for selecting long columns. Once you have determined that you want row 1 through row 56184, you can CTRL-G then enter G1:G56184 and those cells will be selected. I accidentally discovered today that Function Key F5 does the same thing.
I just recently discovered <F4>. I could never figure out how tutorials were magically getting the dollar signs, and I had to type them in. Once you start using <F4> you'll probably not go back to typing the $ signs. If you make a mistake, you can select the cell, and place your cursor in formal expression at the top and <F4> to change the format.

Quote
You must be logged in to read this quote.

I knew I should have put a few pictures in this discussion. I also miscapitalized "Select". I'm using the newest version of Excel. It has a Data tab on the top ribbon. If you click on that, there's a Remove Duplicates icon.  I'll add a picture to explain.[/quote]
I have a Data tab, but still don't see Remove Duplicates.

Logged
jimrtex
Atlas Icon
*****
Posts: 11,828
Marshall Islands


Show only this user's posts in this thread
« Reply #39 on: November 05, 2017, 09:55:02 PM »

A tutorial explaining how you generated these would be useful.

Quote
You must be logged in to read this quote.
I never rename source shapefiles. The census files at least are easy to understand what they are. You haven't actually changed the file by renaming the layer, but it might give the impression that you are.
Logged
cinyc
Atlas Icon
*****
Posts: 12,721


Show only this user's posts in this thread
« Reply #40 on: November 05, 2017, 10:41:35 PM »
« Edited: November 05, 2017, 11:12:55 PM by cinyc »


That’s in the works.

Quote
You must be logged in to read this quote.
I never rename source shapefiles. The census files at least are easy to understand what they are. You haven't actually changed the file by renaming the layer, but it might give the impression that you are.

[/quote]

You have to rename the layers if you use the QGIS print composer. Otherwise, the legend will have the Census Bureau’s name instead of something describing what the color/lines depict. Plus, since Census’ names are very long, not changing them makes the legend a lot wider than it need be.

I’ll mention you haven’t actually changed the filename, just the name in the Legend Panel when I revise things again.  I’ll also take your suggestions for rewriting the first part into account and put in a GOTO description.

I think we’re just going to disagree on whether to include multiple races in the same spreadsheet. My spreadsheets generally include multiple races and even multiple years. It’s much easier to compute things like swings and PVIs that way. Yes, we can do that in QGIS by creating a new column, but I find it easier to do math in Excel instead of QGIS.

One thing I haven’t tried to figure out yet is what happens if you try to import and join an Excel spreadsheet with multiple sheets. Does it import all the sheets or just the first? I’ll have to test that and get back.

Quote
You must be logged in to read this quote.

That's bizarre. I don't think I modified the Excel ribbon at all. The data tab in both my Excel 2007 and Excel 2016 have a Remove Duplicates button. Try right clicking on the Ribbon, selecting Customize the Ribbon and looking for the "Data Tools" group under Commands Not in the Ribbon. Remove Duplicates is in that group.

If its not there, perhaps it's in the Analysis ToolPak add-in, which I have enabled?
Logged
jimrtex
Atlas Icon
*****
Posts: 11,828
Marshall Islands


Show only this user's posts in this thread
« Reply #41 on: November 06, 2017, 01:04:01 AM »

One thing I haven’t tried to figure out yet is what happens if you try to import and join an Excel spreadsheet with multiple sheets. Does it import all the sheets or just the first? I’ll have to test that and get back.
When I did that, it asked which sheets I wanted to use.

I don't have any experience with using xlsx files, since I always used csv files, and I would paste just the columns I wanted to add in the CSV file.

Quote
You must be logged in to read this quote.

That's bizarre. I don't think I modified the Excel ribbon at all. The data tab in both my Excel 2007 and Excel 2016 have a Remove Duplicates button. Try right clicking on the Ribbon, selecting Customize the Ribbon and looking for the "Data Tools" group under Commands Not in the Ribbon. Remove Duplicates is in that group.
[/quote]
OK I found it. It is one of 5 small icons, which you have to hover over.
Logged
jimrtex
Atlas Icon
*****
Posts: 11,828
Marshall Islands


Show only this user's posts in this thread
« Reply #42 on: November 06, 2017, 11:04:18 PM »
« Edited: November 07, 2017, 07:55:30 AM by jimrtex »

This Tutorial Illustrates How to Edit Shapefiles. In the previous tutorials we did not change the shapefiles, we only changed how they were rendered. The joins were done in memory. When we save a project, we are saving all the steps that we had performed to reach a particular point.

(1) Start with a new project. You can use an existing project, but it is probably less confusing to start fresh.

(2) Drag the county shapefile cb_2016_us_county_5m.shp into the map area.

(3) Change the On The Fly project to Alaska_Albers_Equal_Area_Conic,

(4) Use the Zoom In tool to focus on Alaska.

We want to keep the original layer, so we make a copy.

(5) Duplicate the county layer by right clicking on the layer, and selecting Duplicate.

(6) Save the (duplicate) layer to disk by right clicking on the layer, and selecting Save As. Give it a name like Alaska_Counties. Be sure to browse to the directory where you are going to save the file and be sure the format is ESRI Shapefile

You might now remove the other layers, so as to reduce confusion. Note that we have not created a layer with Alaska counties only, but rather a duplicate of our original shapefile. You can confirm this by panning to the southeast.

Make sure that your new layer is the current layer (click on it)

(7) Select the area we want to keep.

Using the Select by Expression tool, construct the expression:

    "STATEFP" = 2

You can type this directly, or find the variable STATEFP under Fields and Values and click on it. The double quotes around STATEFP is part of the expression syntax. Click on the = sign, or type it is, and type in 2. The STATEFP for Alaska is 2.

Click on Select, and Alaska should be highlighted in canary yellow. We have not selected Alaska, but rather all the county (equivalents) in Alaska.

If you are doing a different state and don't know the STATEFP, use the Information tool and click somewhere in the state. For example, the STATEFP for Wisconsin is 55. Once you have the information you need, click on the X in the upper right corner of the information panel to eliminate the clutter.

You can also do a graphic selection.

The selection of the counties in the proposed state of Southern California were selected using the Select Features by Freehand.



The selection of the counties in New England were selected using Select Features by Polygon. It also can be done with Select Features using expression.

    "STATEFP" = 9

selects Connecticut. Click on Add to Selection. Then change the 9 to 23 (Maine) and Add to Selection. Repeat for Massachusetts (25), New Hampshire (33), Rhode Island (44), and Vermont (50).



If you make a mistake with your selection, click on Deselect Features and try again.

(8) Invert your selection by selecting Invert Feature Selection (under Select Feature by Expression)

If you are working with Alaska, pan to the southeast to make sure that the continental US is now highlighted in Yellow.

(9) Click on the Pencil Icon in the upper left. This enables editing for the current layer. When a layer is enabled for editing, all the vertices are highlighted with red lines.

Double check that the current layer is the one you want to edit.

(10) Delete the selected areas by pressing Delete on the keyboard (or Edit > Delete Selected)

If this was not what you wanted to happen, click on CTRL-Z to undo the last edit, then click on the the Pencil Icon and go back to Step 7.

(11) Click on the Pencil Icon to complete the editing of the layer. You will be asked if you want to save the edits. Confirm that you do. Note the Pencil Icon is a toggle, it is used to both start and ending editing.

(12) Check that the editing worked. The deleted areas will have disappeared. Click on the Vermont Avenue icon, and scroll down. You should only see the 29 Alaska county (equivalents).

The shapefile that you have just created, can be used in place of the US Counties shapefile that we used in the previous tutorials.
Logged
cinyc
Atlas Icon
*****
Posts: 12,721


Show only this user's posts in this thread
« Reply #43 on: November 07, 2017, 01:29:35 AM »

(6) Save the layer to disk. Give it a name like Alaska_Counties. Be sure to browse to the directory where you are going to save the file and be sure the format is ESRI Shapefile

You probably should explain how to do this - Right Click on the layer name and Select "Save As" or Layer-->Save As.

I don't see any button on the toolbars to save a layer, but I tend to keep toolbars off unless I need them. I'd rather have the maximum map on the screen to work with.
Logged
jimrtex
Atlas Icon
*****
Posts: 11,828
Marshall Islands


Show only this user's posts in this thread
« Reply #44 on: November 07, 2017, 10:23:32 AM »

Editing Features With QGIS.

We will be creating an outline map of Alaska, by merging all the counties.

(1) Begin with the Alaska_Counties layer created in the previous tutorial.

(2) Duplicate the layer, by right clicking on the layer, and selecting Duplicate.

(3) Save a copy of the layer, by right clicking on the layer and selecting Save As. Name the layer Alaska_Outline

Be sure to browse to the directory you want to save the shapefile in, and that format is ESRI Shapefile.

To reduce clutter and confusion, remove the other layers (right click on the layer(s) and select Remove). Note you may select multiple layers and remove all simultaneously.

If you are making several edits, it is not necessary to do steps (2) and (3) each time, but it is a good idea to save the starting source layer, and perhaps checkpoints along the way.

(4) Select the features to be merged. Since we are merging all the county (equivalents) in the state, under Select By Expression, Select All Features. The entire state should be yellow.

(5) Click on the Pencil Icon to begin editing. The map will grow red fur.

(6) Edit-> Merge Features. If you do this a lot, you might want to add a tool icon to the Tool Bar.

A table of the selected features will be displayed. Select one. I selected Juneau, but it doesn't really matter. You could pick Mat-Su or some other county (equivalent). The selected area will be orange. Click on Take Attributes from Selected Features, and click on OK. The county boundaries will disappear.

(7) Click on the Pencil Icon to complete editing of the layer. Confirm that you want to save the changes.

(8) Click on Vermont Avenue icon, and you will see that there is now only one row. The information was copied from the table row that we selected in step 6. It is mostly useless. For example the land and water areas are for Juneau, and not the state. For illustration purposes, we will change the name of our feature to Alaska.

(9a) With the table open. Click on the Pencil Icon on the top left edge of the table. This is just a duplicate of the icon on the toolbar.
(9b) Click on the field you want to change (NAME Juneau)
(9c) Type Alaska.
(9d) Click on another field to register that the field has been updated.
(9e) Click on the Pencil Icon.

We can also create some simple displays.

(10a) Right click on the layer select Properties -> Labels.
(10b) In the top pulldown select Show Labels For This Layer
(10c) Select NAME in the next pulldown.
(10d) Click OK.

Alaska is now in the middle of the feature (the entire state). If we had not edited the name in step (9), the label would say Juneau.

We could also have selected the Script-E on the label menu and entered an expression such as 'Not Kansas' as the expression and typed OK. In expressions, literal strings are in single quotes, field names are in double quotes.

We can combine layers.

(11) Drag the Alaska_Counties shapefile into the map, and in the Layers Panel drag it below the Alaska_Outline layer. Since the Alaska_Outlines is on top, it hides the Alaska_Counties. Uncheck the Alaska_Outline layer and we can now see the Alaska_Counties.

To Be Continued


Logged
🐒Gods of Prosperity🔱🐲💸
shua
Atlas Star
*****
Posts: 25,663
Nepal


Political Matrix
E: 1.29, S: -0.70

WWW Show only this user's posts in this thread
« Reply #45 on: March 26, 2018, 09:53:08 PM »

This is really cool, thanks for doing this.

If I wanted to use a different series of color gradients to visualize the wincodes, as opposed to the standard Atlas colors, is there a way to do this?  I know how to make a single simple gradient in QGIS to portray a field, but I want to know if there's a way to combine them.
Logged
🐒Gods of Prosperity🔱🐲💸
shua
Atlas Star
*****
Posts: 25,663
Nepal


Political Matrix
E: 1.29, S: -0.70

WWW Show only this user's posts in this thread
« Reply #46 on: March 27, 2018, 01:12:46 AM »
« Edited: March 27, 2018, 01:19:22 AM by shua »

This is really cool, thanks for doing this.

If I wanted to use a different series of color gradients to visualize the wincodes, as opposed to the standard Atlas colors, is there a way to do this?  I know how to make a single simple gradient in QGIS to portray a field, but I want to know if there's a way to combine them.

Ok, I figured a relatively simple alternative.

What I did was create a selection for each plurality, and then turn each of those into a separate shapefile and layer them on top of each other.

I'm working with race/ethnicity so I have a layer joined to race/Hispanic census tract info. I start off representing this with a gradient spectrum for "% Non-Hispanic Whites" that I will layer the other categories over.  I click on this layer under "Layers" , and go to "Open Attribute Table."  Then on the Attribute Table I click on the button on the top with the epsilon () "select features using an expression."     

Then type in the expression, for example:
Quote
You must be logged in to read this quote.

Then I press the button in the lower-right corner of that window with the epsilon that says "select."

This will select all those tracts where the number of Hispanics is greater than the other categories.

So with those selected, it should say at the top of my attribute table how many features (census tracts) are selected. I go back to the main window, click on the layer that I selected from, and find "Save As..."  On the "Save vector layer as..." window that pops up, I click the "browse" Button to make sure I am saving the new layer where it needs to be on my computer.   Be sure to select the tiny button that says *"Save only selected features"*

 And then, so long as I have "Add saved layer to map"  clicked, a new layer will show up on the map, that will be only those census tracts where Hispanics are a plurality.

Since I am working with a table, I need to be sure to Join the table to the layer, by going to "Joins" in the "Layer Properties" window (with the correct Join and Target fields - I'm using GEOId2 and GEOID respectively).  Then I can go to the Style under Layer Properties, choose "Graduated" at the top, choose the field I want to represent with that layer (in this case %Hispanic) under "Column," and choose a color ramp and then make adjustments to it to make it look how i want it.

Just do this for each category, and then layer them on top of each other on the map.
Logged
cinyc
Atlas Icon
*****
Posts: 12,721


Show only this user's posts in this thread
« Reply #47 on: March 27, 2018, 08:00:05 PM »

That's one way of doing it. You can also create a wincode and use QGIS to create discrete or continuous graduated color ramps. You can do that in 3.0 by selecting Symbology from the properties menu, then selecting Graduated from the drop down menu. Clicking on the down arrow next to the color ramp will allow you to select color ramps.

QGIS has a series of color ramps to choose from (look under "All Color Ramps" for the complete list, or you can make your own).

Making your own color ramp is a bit complicated (QGIS' symbology is probably its weakest point). The easiest way to do it would probably be to load the 8 color ramp and individually change the colors in the tabs. Load the 8 color ramp and click on the colors next to the color ramp. The first color stop is the dimmest color in the 0-1 range; the second is the brightest. The third color tab is the dimmest in the 1-2 range; the fourth is the brightest, etc.  The stops are set at 0%,9.9%;10%,19.9%:20%,29.9%, etc.

Then, just calculate a WINCODE for each plurality racial group.
Logged
Krago
Jr. Member
***
Posts: 1,081
Show only this user's posts in this thread
« Reply #48 on: February 06, 2022, 02:24:34 PM »

Sorry to revive this thread, but I need some help.

I have spent many happy years making political maps on MapInfo.  Now I'm trying to learn QGIS, and I'm ready to pull my (remaining) hair out!

Here's a simple task that I can do in less than a minute in MapInfo, but I can't figure out how to do in QGIS after hours of Googling.

I want to split a polygon in Layer A using a polygon in Layer B.

In MapInfo, I make Layer A editable, select the polygon(s) on Layer A, and click on SPATIAL > Set Target.  I then select the polygon(s) on Layer B, click on SPATIAL > Split > Split Target, select the values on 'Data Disaggregation', and click OK.

What the hell do you do in QGIS?
Logged
cinyc
Atlas Icon
*****
Posts: 12,721


Show only this user's posts in this thread
« Reply #49 on: February 06, 2022, 02:42:12 PM »

Are you trying to do a difference or a true split?

The easiest way to split is probably selecting the 2 polygons and using the Intersection tool from the Processing Toolbox to do an intersection. This should split the polygons along the old lines.

Alternatively, you can transform the polygon in Layer B and split by the polygon in Layer A by line using the Split by Line tool making sure the line overlaps the edge of the polygon.

There are also advanced processing tools that might allow you to do it manually, plus a few plugins (like Split Features on Steroids) that might help.
Logged
Pages: 1 [2] 3  
« previous next »
Jump to:  


Login with username, password and session length

Terms of Service - DMCA Agent and Policy - Privacy Policy and Cookies

Powered by SMF 1.1.21 | SMF © 2015, Simple Machines

Page created in 0.116 seconds with 14 queries.