Useful Tip: Copy-Paste Geometry from QGIS to PostGIS

Did you know (I just found out) that when you copy some rows from your attribute table in QGIS and paste that into a Query window in PostgreSQL, the geometry of your object is passed as WKT geometry?

Try this:

Open QGIS, select some geometries and copy the selected rows to the clipboard (Ctrl-C or by using the Select button in the Attribute table window)

CopyQGIS

Then, open PostgreSQL, open a Query window and paste your data:

PastePostgres

The first column is your geometry in Well-Known Text!

Could be uber useful and easy after that to simply use this geometry description in association with ST_GeometryFromText to perform many type of analysis (like using those polygons to perform an Intersect) without the need to import your data into PostGIS…

Just thought you’d be interested to know! :)

Posted in GIS, PostGIS | Tagged , , | 8 Comments

How to add custom tiles as a QGIS background

This post was made using QGIS 1.8 (or so…). It will not work on 2.4.

Yesterday, Nicholas Duggan (@Dragons8mycat) tweeted about a project he did with his kids over the week-end. He created a “Treasure Map” with them in QGIS using the OpenLayers Plugin that allows you to use OSM, Google, Yahoo or Stamen as a background. I told him that he could have use Mapbox’s Pirate Map as a background which would be cool (that was before discovering that the map zooms from level 2 to 6 which is unsuitable for a local treasure map). For the example, I’ll create a custom map on Mapbox and use it.

FIRST STEP: Read Nicholas’ blog post.

It will tell you how to install QGIS and the OpenLayers Plugin.

 

SECOND STEP: Create your custom map

Go to mapbox.com and open an account if you don’t have one. It’s free!

1. Pricing  MapBox - Google Chrome_2013-05-29_07-59-48

 Choose the free account. The basic account (at 5$/month, it’s as good as free) gives you the best imagery around. It’s particularly good if your are in the United States or in Europe because you have up to zoom level 19. In Canada, where I live, we have for now up to level 15.

BostonCommonMontreal

For now, the free account is all you need.

Login into your newly created account and create a new map.

NewMap

 

There’s some preset background that are really good from which you can start (I can choose the satellite background because I have the basic account).

NewMap2 And then, you can modify it.

PresetYou can use any of the predefined color scheme to create a distinctive look (like that orange palette) or keep it like that.

Preset2

For the tutorial, I’ll use a customized one, so I’ll click on the second tab “Customize”. There, you can adjust the color of almost everything on the map. Let say we want to create a map for the upcoming Father’s Day so we’ll create a map in the palette of our father’s favorite brown jacket. We tweak the colors a little bit to create a nice ugly brownish map.

CustomThat way, it will be easy to see that the map inserted in QGIS in the end is really the one we created ;)  Click “Save” and then “Publish”.

PublishCopy the link at the top in your favorite text editor.

 

THIRD STEP: Modify the OpenLayers Plugin.

Go to the folder where your QGIS plugins are installed and navigate to the OpenLayers Plugin. It should be: “C:\Users\your_user_name\.qgis\python\plugins\openlayers_plugin”

Open the file file “openlayers_plugin.py” in your favorite text editor and find the lines where all the available backgrounds are listed.

OpenlayerPluginPyCopy and paste the last line and modify the last part so it will contain information about your custom background. I put “Mapbox Custom Brownish”. Use a .png that I created from mapbox logo (the .png should be in the same folder as your .py file). Give the name “mapbox_brown.html” to the page that will be displayed as a background in QGIS (we are getting there just now). Save and close.

Now, navigate to the “html” subfolder that is in the “..\plugins\openlayers_plugin” folder (that would be “C:\Users\fgoulet\your_user_name\python\plugins\openlayers_plugin\html”. Copy and paste one of Stamen’s html file. Don’t use Google’s or Yahoo’s because they reference Google’s and Yahoo’s map api. The Stamen background are using all OpenLayers’ .js and .css which is what we want. I use “stamen_terrain.html”.

Go to line 30ish where there’s the declaration of a variable called “apple”.

ToChangeand change the first string to “Mapbox Custom Brownish” or whatever name you gave your map. In place of “http/tiles.stamen.com/terrain”, copy the link you copied earlier, without the last part “page.html”, but keep the “/${x}…” till the end of the line. That’s the reference to your tiles in your account. You should have something like “http://a.tiles.mapbox.com/v3/fgcartographix.map-8y8f5h86/${z}/${x}/${y}.jpg”.

Changed

Save and close.

 

FOURTH STEP: Add your tiles as a background.

Open QGIS. Click Plugins>OpenLayers plugin and you’ll see at the end the name of your map. Click on it!

QuantumGIS

Voilà! Your custom map appears in the background. Add your own data if you want. I work for a transit agency so I’ll add our train lines for the example. You can even change the projection. The tiles will be automatically reprojected.

QuantumGIS_final Here you go. You can now add as many custom background as you want. You could use TileMill to create custom maps, upload them on your mapbox account and use them as a background.

Now, if I could just find the time to create a local Pirate map…

 

 

 

 

 

 

 

Posted in Uncategorized | 10 Comments

Newest map (with texture shading!)

I just received some pictures about my latest “creation”.

I created a relief map of Haiti for the Canadian Museum of Civilization exhibit “VODOU“.

The map is displayed in the entrance corridor as an introduction to the exhibit.

Corridor 3

The particularity is that I used a “new” technique by Leland Brown called Texture Shading.

Contrary to a standard shaded relief, texture shading shows the relative height of each point relative to the terrain nearby. It will show small ridges and canyon that are important in their relative environment, even though they aren’t in the whole area… You can read everything about it here (15mb pdf) and take a look at the software

Corridor 6

 The final design was done by a design firm, but I’m really pleased with the result! :)

 

Posted in Cartography | Leave a comment

The Trivariate Flow Map Problem.

Yesterday I asked on Twitter how to solve a problem using trivariate flow maps. Of course, explaining it all with 140 characters was quite impossible.

Here’s the problem (due to confidentiality, the values aren’t the true ones, but you’ll get the idea).

So, the problem is to visualize the flow of movement from and to a region in a certain territory, the use of public transportation and the possible clients that aren’t taking it. There’s 8 regions so we will have 8 arrows.

For now, here’s the variable:
Total movements : Number of movements from the study area to the other regions.
Use of public transit : Of all the movements from and to the study area, the ratio that are made using public transportation (%)

This is what we have (click to enlarge):

This map tells us that between 30 and 40K people are going from region 6 to region 2 and than 1 in 4 are sing public transit. A little less is going to region 1, but almost 1 in 2 movement is made with public transit. Which is good. Region 4 is interesting. There’s 10-20K people going there, but less that 1 in 5 is using public transportation. There’s room for improvement. Maybe.

There’s many reason that can influence car ridership instead of public transit. On of them is the number of stop you have to made. If you move from you home to work and in the evening from work to home, this is what we call a pendulum movement. Those people are easier to convince to use (or at least try) public transit. Once they know the schedule and route to take.

On the other hand, if you go normally from home, then drop a kid to school, the go to work, the in the evening pick kid from school, go to the grocery store, drop the kid to soccer practice before coming home, it’s more difficult. So, we have identified those making pendulum movements.

Now, how can I add to the same arrows (ideally), a symbology that can represent the ratio of car users that are making pendulum movement?

For now, I think of using a bivariate color legend:

The arrow will be colored using this chart so the darker the color, the more pendulum movements are made on this axis. If also, the use of public transit is low (yellow or red), the more clients we could hope to convince…

What do you think?

Posted in Cartography | 4 Comments

La boîte à outil du cartographe / Cartographer’s Toolkit

J’étais excité hier comme un écolier qui apprend qu’une tempête de neige s’annonce et qui réalise que l’école va probablement être fermée le lendemain! ;)

J’ai reçu ma copie de «Cartographer’s Toolkit» par Gretchen Peterson de PetersonGIS.

Dans ce livre, Gretchen a trouvé des solutions pratiques aux problèmes fréquemment rencontrés par les cartographes : le choix des couleurs, la typographes ainsi que le style de carte et la mise en page.

Sans tomber dans la théorie lourde – ce qu’elle ne laisse pas de côté d’ailleurs – Gretchen se concentre sur des exemples concrets pour nous inspirer. Elle a créé 30 palettes de couleurs (coordonnées, continues et différenciées), montre une cinquantaine d’exemples de polices de caractères – en contexte – et 28 types de cartes et d’embellissement possible le tout accompagné de 36 cartes «par plusieurs cartographes “dominants” d’aujourd’hui» (dont votre humble serviteur ;)).

C’est le livre à avoir pour toute personne qui aspire à devenir cartographe ou pour tout cartographe qui pourraient, un jour, être en manque d’inspiration.

Même si l’anglais n’est pas votre langue préférée pour la lecture, vous pourrez profiter de la quantité incroyable d’informations utiles contenuse dans ce livre.

Pour vous procurer le livre : Amazon.ca
Pour suivre Gretchen sur Twitter : @PetersonGIS

Posted in Cartography, GIS | Leave a comment

My First Python Script!

Wooohoooo!

Ok, it took a incredible ridiculous amount of time to create it, but I finally did it!

CREATE NETWORK FLOW ARCS

The script is quite simple. It takes a shapefile with an ID, and origins and destinations coordinates in it and calculate from a given index, a curve between the origin and destinations. That’s it. It don’t do much, but it answered a need I had so…

HOW IT WORKS

The arguments on the command line must be: >python FlowArcs.py [C:/Path/Shapefile.shp] [curvature index]

The source shapefile must be structured like that: id | FromX | FromY | ToX | ToY


The script use the values in the shapefile to create the arcs, not the geometry of the points. The order is important because it search for the position of the field, not its name.

The curvature index determine the “roundness” of the arcs created. With an index of 1 (there’s a mistake on the picture below), you will have a perfect half circle and the higher the index, the flatter the arcs will be. It depends of your data, but with our tests, an index of 10 is usually good.

The output will be in a “Arc” directory, in the same directory as your source shapefile.

For those interested in the “principle” behind it:


You’ll need GDAL-OGR and Shapely in order to make it works.

You can download the file here:   FlowArcs.py

Licence Creative Commons
Ce(tte) œuvre de Francois Goulet est mise à disposition selon les termes de la licence Creative Commons Attribution 4.0 International.

Posted in GIS, Python, Script | Tagged , , | 2 Comments