Results 1 to 8 of 8

Thread: How do you programmtically determine regions of a world map?

  1. #1
    Join Date
    Apr 2014
    Posts
    34
    Thanks
    14
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11

    Default 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?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How do you programmtically determine regions of a world map?

    QPolygon or QPolygonF perhaps?

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    SpiceWeasel (27th November 2015)

  4. #3
    Join Date
    Apr 2014
    Posts
    34
    Thanks
    14
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11

    Default 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!

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,239
    Thanks
    303
    Thanked 865 Times in 852 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default 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 :-)

  6. The following user says thank you to d_stranz for this useful post:

    SpiceWeasel (27th November 2015)

  7. #5
    Join Date
    Apr 2014
    Posts
    34
    Thanks
    14
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11

    Default 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!

  8. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,239
    Thanks
    303
    Thanked 865 Times in 852 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How do you programmtically determine regions of a world map?

    QGraphicsSvgItem
    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.
    Last edited by d_stranz; 27th November 2015 at 21:17.

  9. The following user says thank you to d_stranz for this useful post:

    SpiceWeasel (1st December 2015)

  10. #7
    Join Date
    Apr 2014
    Posts
    34
    Thanks
    14
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11

    Default 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.
    Attached Files Attached Files

  11. #8
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: How do you programmtically determine regions of a world map?

    Quote Originally Posted by SpiceWeasel View Post
    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.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

Similar Threads

  1. Replies: 2
    Last Post: 24th April 2011, 14:14
  2. QGraphicsTextItem is blurring at certain regions
    By wagmare in forum Qt Programming
    Replies: 12
    Last Post: 22nd July 2009, 12:03
  3. Clickable Regions
    By bbdaffy in forum Qt Programming
    Replies: 6
    Last Post: 22nd July 2009, 11:26
  4. Replies: 13
    Last Post: 18th February 2008, 00:20
  5. Paint MainWindow Regions
    By pedros09 in forum Qt Programming
    Replies: 1
    Last Post: 5th May 2006, 09:43

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.