How do you programmtically determine regions of a world map?
I am trying to figure out how to build a game like Risk. What I am focusing on now is just the map, setting it up in a scene, visualizing in a view, and using the mouse to click on regions and have it print out what region was clicked on.
My question is, how to make a regions of a map (like the world map -- not simple rectangles) identifiable to the program?
Re: How do you programmtically determine regions of a world map?
QPolygon or QPolygonF perhaps?
Cheers,
_
Re: How do you programmtically determine regions of a world map?
Thanks, I've read about those classes but not sure how to apply it to the image of a world map (ala Risk). Even making the problem simpler, say using an image of the US states map, how would you create those polygons? Is their a class or tool that does that?
Oh, and BTW, Happy Thanksgiving to all!
Re: How do you programmtically determine regions of a world map?
You might try this: I googled for "US map state outlines" and got to the d-maps.com site, and specifically to a page with a blank map of the US state outlines. In there you will see a downloadable map in SVG format (which is XML for graphics). If you look at the XML source, you will see 50 or so "<path>" elements, each of which contains the polygon coordinates for a state (plus the outlines for parts of Mexico and Canada).
You'll need to read some SVG documentation to figure out how to decode the SVG, and you'll probably have to do some figuring out of which polygon is which, but that will give you the dimensional data you need to construct QPolygonF instances for each state.
Start with the US, and before long, you'll be able to rule the world :-)
Re: How do you programmtically determine regions of a world map?
Thanks very much!
I've started looking over the SVG file format and the QGraphicsSvgItem class. The SVG file format is pretty cool!
Re: How do you programmtically determine regions of a world map?
Yes, I'd forgotten about that. You might be able to simply take apart the map SVG file and create separate SVG files that you can use to create a QGraphicsSvgItem for each region. That would fit nicely into a Graphics / View architecture for the game board and player pieces.
QGraphicsSvgItem is a bit restrictive, though. If you're designing a Risk-like game, you might, for example, like to be able to change the color of a region depending on which player has control of it. You can't do that with the SVG item - its properties and appearance are hard-coded into the SVG file it uses as a source. Reading the SVG as XML instead, and creating a QGraphicsPolygonItem from the path coordinates will give you greater control over dynamic properties.
1 Attachment(s)
Re: How do you programmtically determine regions of a world map?
Thanks again, anda_skoa and d_stranz!
Because of your input, I managed to create an application showing a map of the US! I used QGraphicsScene with QGraphicsPolygonItems which I create from data that I load from an SVG file. There are some minor issues with the map -- the tool to convert a <path> elements to <polygon> elements (https://betravis.github.io/shape-tools/path-to-polygon/) did not create a perfect reproduction. I need to spend some time looking into that.
The coolest thing about doing this was actually having to delve into the SVG format itself. Who knew you could represents graphics by textually enumerating a path? I've included the code, just in case someone else is interested in this.
Re: How do you programmtically determine regions of a world map?
Quote:
Originally Posted by
SpiceWeasel
Who knew you could represents graphics by textually enumerating a path?
The "V" in SVG stands for vector... :) That's the entire reason SVG files are resizable up or down w/o losing resolution and why it's the chosen file format many applications where something needs to be drawn, etc.