473,473 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How can I select a country from a map?

27 New Member
Gentle Community Members,

I want to display a form with an image, the map of Europe.
How can I achieve that the event handler displays the information belonging to the selected country.

I have discarded the usage of complicated polygon and elliptical shapes.

I have found out another way. I download a map with borders of countries. I fill each country with unique different color. If the user clicks onto a point then the event handler reads the color of it. I will include a table in my program in order to select the country having the chosen color and display the information connected to this country.
Expand|Select|Wrap|Line Numbers
  1. SELECT COUNTRIES.INFO1,COUNTRIES.INFO2 FROM COUNTRIES WHERE COUNTRY.COLOR IS [COLOR_OF_PIXEL_X_Y]
  2.  
How do you like this idea?

I am a beginner in C#.
Please help me in event handling and table generation and queries.

I have found a map and I colored it. I collected country codes, names and RGB triplets in an Excel table.
Regards
Frank
Attached Images
File Type: jpg Europe.jpg (126.7 KB, 9888 views)
Attached Files
File Type: txt Europe.txt (1.1 KB, 1084 views)
Oct 27 '11 #1
10 7075
GaryTexmo
1,501 Recognized Expert Top Contributor
Personally I would advocate the usage of polygons only because the algorithm for detecting if a point is inside a polygon is actually not that bad, and because with this way you're limited to a set of colours. Having said that, RGB space has a ton of colours so you're probably ok.

If you're wondering which event to use, you should probably look into the MouseMove event. I believe the event args will give you a position which you can then use to map back to your image. A Bitmap object has a GetPixel method, which will return the Color at that coordinate. If you're drawing with any scaling/filtering, you'll need to take that into account... so maybe get it working with an unscaled bitmap for now.
Oct 27 '11 #2
franknagy
27 New Member
Thank you for the suggestions, Gary.
Where can I find the algorithm for detecting whether a point is inside a polygon?

Now I have a database of European Countries and the corresponding map.
Expand|Select|Wrap|Line Numbers
  1. Code    Name    Red    Green    Blue
  2. ~~    tenger    221    221    221 <--sea
  3. AD    Andorra    255    0    40
  4. AL    Albánia    192    128    255
  5. AT    Ausztria    0    210    255
  6. BA    Bosznia    234    0    234
  7. BE    Belgium    99    255    32
  8. ...
  9.  
The polygons determining the borders European countries are very complicated. I started from a gray scale map, and colored the >40 countries. The problem was that some borders were not continuous and the filling spilled over them.
As you can see, I copied the RGB weights from the color editor window of Irfan view. How can I compare RGB triplets with pixel colors? Has the Pixel.Color object built-in Read, Green and Blue properties, or have I myself to count the bytes from the equations
Expand|Select|Wrap|Line Numbers
  1. Color = 256*(256*Red+Green)+Blue or
  2. Color = 256*(256*Blue+Green)+Red
?

Regards,
Frank
Attached Images
File Type: jpg European_countries_and_cities_colored.jpg (150.2 KB, 11351 views)
File Type: jpg Europe_grayscale.jpg (118.3 KB, 3547 views)
Oct 28 '11 #3
GaryTexmo
1,501 Recognized Expert Top Contributor
The algorithm for detecting if a point is inside a polygon is a matter of picking a point outside the geometry and creating a line between that point and the click point. A point is inside the polygon if that test line intersects with your polygon an odd number of times. That is, test each line segment of your polygon and every time that test line intersects, increment a counter. If your polygons are complex, you might want to implement some sort of QuadTree or other space partitioning algorithm so you can more quickly get from your click to your polygon without having to test every single one. I have a basic QuadTree algorithm posted in the insights section here: http://bytes.com/topic/c-sharp/insig...implementation

You could contain your polygons by their bounding box and insert them as such into the quad tree. Use your mouse's click location to give you the closet polygons to the click and then you can test those for intersection. You mentioned your polygons are complex, but I doubt you'd be looking at more than 1000 intersection tests if you use the quad tree and today's processors should handle that no problem.

As I mentioned before though, this is only if you want really accurate detection. Your colour method should work, you just might run into a few troubles with border detection (as I noticed you coloured all the borders in yellow), if you need to add more countries, or if you decide you want to change the colours (though you can use a hit detection map and a draw map).

So with regards to your second question of how to compare the colours, you don't really need to get that complicated. As I mentioned, the Bitmap.GetPixel(x, y) method will return a Color object. You can use this to directly compare against the colours you have.

Here's an example assuming you had a Point called clickLocation, a Bitmap mapImage, and a list of Country obects that have a MapColor property (that you'd have loaded from the database or file)...

Expand|Select|Wrap|Line Numbers
  1. List<Country> countries = /* list of countries */
  2. Bitmap mapImage = /* map image bitmap */;
  3. Point clickLocation = /* mouse click location */;
  4. Color pointColour = mapImage.GetPixel(clickLocation.X, clickLocation.Y);
  5.  
  6. Country hotCountry = null;
  7. foreach (Country c in countries)
  8. {
  9.   if (c.MapColor.R == pointColor.R && c.MapColor.G = pointColor.G && MapColor.B == pointColor.B)
  10.   {
  11.     hotCountry = c;
  12.     break;
  13.   }
  14. }
  15.  
  16. if (hotCountry != null)
  17. {
  18.   // We've found the country that was clicked on
  19. }
Oct 31 '11 #4
franknagy
27 New Member
Gary,
I have returned from a week of vacations. This time I was not dealing with neither e-mails nor C#. I see some problem with my colored countries:
- there are country codes and city names written in grey within the countries,
- there are some white pixels especially at the borders of countries.

I will return to the topic next week.

Regards
Frank
Nov 4 '11 #5
GaryTexmo
1,501 Recognized Expert Top Contributor
No problem :)

And as I mentioned, there are two ways you can deal with those problems...

1) Use bounding polygons
2) Use a hit map image with the colours to detect where the cursor is but draw the map to the screen. You will have duplicate image data here but it's a simpler implementation than 1.
Nov 4 '11 #6
franknagy
27 New Member
Dear Gary,
Your suggestion that I should maintain two maps is very good.
First map is for the user, with country names and cities; second map for internal usage without them and probably without boundaries themselves.
As usual, an answer leads to a new question.
How can I make the second invisible map from the first visible map?
Recommend me a graphical program, and a menu point on it, to effectively recolor the letters and bytes occasionally remained white to the unique color of the embedding country.

Regards
Frank
Nov 5 '11 #7
GaryTexmo
1,501 Recognized Expert Top Contributor
You don't really need to display both maps... just do all your checks against one but actually draw the other.

I'm afraid I don't know a good program to do that specifically, but were I to do it I would probably just do it manually with mspaint or Paint.NET. The latter is a free tool you can get that has a good set of features... their website is here: http://www.paint.net/

Good luck!
Nov 7 '11 #8
franknagy
27 New Member
Hi,
I am in the stage that the coordinates and the color weights are displayed below in the text boxes under the map.
Next TODO:
I have to select the record in the country grid based on the color codes.
Problem: Why is the record selector on the top of the form instead of its natural place under the data grid?
Further TODO:
Display subset another data set: Travel goals within the selected country.
Captured part of my form: http://http://franknagy.atw.hu/temp/travel.png
Regards
Frank
Nov 8 '11 #9
GaryTexmo
1,501 Recognized Expert Top Contributor
I'm not entirely sure what your question is... if it's the one about the record selector not being where you expect, I can't really say. It's your code, you may need to do some debugging if it's not behaving how you expect it to.
Nov 8 '11 #10
franknagy
27 New Member
Thank you Gary.
I think that I can close this thread. Maybe I start a new one in the folder of C# database manipulations.

Regards
Frank
Nov 9 '11 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: John C | last post by:
I am working with an HTML-based form that uses a select element that requires about 200 options. Is there a way that I can load, or select one of these options from a file, rather than hardcode...
18
by: CJM | last post by:
I'm building a search function for one of my applications. The user has the option to enter a number criteria of criteria, but none are compulsary. I need to be able to build up a query string that...
0
by: aPRSComp | last post by:
Hi, Has anyone seen where using SELECT * truncates characters ? mysql> SELECT * FROM countries; +-------------+-----------+ | country | capital | +-------------+-----------+...
2
by: PRS | last post by:
Why does 'SELECT * FROM countries' truncate characters and SELECT country FROM countries does not ? mysql> SELECT * FROM countries; +-------------+-----------+ | country | capital |...
9
by: Udo Marx | last post by:
Greets to ciwah! I'm doing a little webproject for a local session event. Tryin' to meet latest standards i failed to do this: --snip-- <select name="fromcountry" accesskey="l" title="+">...
1
by: serena.delossantos | last post by:
Trying to insert into a history table. Some columns will come from parameters sent to the store procedure. Other columns will be filled with a separate select statement. I've tried storing the...
2
by: ssjay1981 | last post by:
Hello All, I am new to ms-access, i have a table with values as mentioned below. Each of these table are given with a list of Look up values, in order to narrow down the list of values under...
3
by: SSG001 | last post by:
i have two seelct boxes in one row as below </form> <table> <tr><td> <select name="countrycode" id="countrycode" onchange="showcountrycode (this.value);"> <option selected>select...
2
OuTCasT
by: OuTCasT | last post by:
Hi. I have 6 dropdownlist boxes. Industry Province Classification Status City Country
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.