3

I am building this tool for emergency care at events, and I am stuck in a problem. I have this map, and there are zones marked for a certain team to intervene if something happens in their area.

Now, when creating an intervention, it is possible to enter a coordinate on the map (x,y). I want to find out how I can do this.

Zones for teams are not necessarily squares. I have them stored like this

x1,y1,x2,y2,x3,y3,x#,y#.

What do you think would be best practice?

  1. Adding 4 fields, start_x, end_x, start_y and end_y and then do a SELECT * FROM tble WHERE coord_x BETWEEN start_x AND end_x query to get the X side, and same for the Y side.
  2. Do the same like above, but instead of adding fields, just do this in PHP code, run through every team area
  3. Your solution?

Thanks in advance :)

JJ15
  • 201
  • 2
  • 16

2 Answers2

1

You should devide each area defined into triangles. Then you can use an approach as mentioned here: How to determine if a point is in a 2D triangle?

Like this you don't have to wrap your head around complex shapes with mutliple edges and corners - cause EVERY shape with an finite amount of corners can be divided into triangles.

Another approach that might be possible:

Assuming you are defining the shapes BEFORE any incident may occur. Assuming those shapes do not CHANGE during the possible time:

  • Store the Shapes in a format, compatible to the HTML map-tag, and associate them with a team responsible: http://www.w3schools.com/tags/tag_map.asp
  • When the reporter creates a incident, don't use the coordinates to determine the team, but the shape he clicked on the defined map. Then you don't have to resolve any points / area borders at all.
Community
  • 1
  • 1
dognose
  • 20,360
  • 9
  • 61
  • 107
  • Hey, yes image maps is one approach, but it should also be possible to choose a location from a list (in the create intervention field) and then it should give the nearest team - so for this image maps does not work - but choosing from the map image maps is perfect! thanks :D – JJ15 Jul 03 '14 at 18:44
  • 1
    @JELLEJ Well, those locations also need to be defined - and if you predefine them, using "x=324,y=24t53" or "relatedShape=blueTeam" - shouldn't matter, unless you can reuse defined locatiosn with changing shapes... – dognose Jul 03 '14 at 18:49
  • I've chosen your solution as my solution. Thanks! – JJ15 Jul 03 '14 at 22:07
1

If you can use PostgreSQL/PostGIS then you can define your zones using Polygon geometry column. Then it is trivial to select the zone using a point coordinate using the ST_Contains() PostGIS function.

Equivalent functionality exists in MySQL with Spatial Extensions. Here is a quickstart guide for MySQL.

EDIT: A quick Mysql Implementation

CREATE TABLE `geozones` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(50) NULL DEFAULT NULL,
    `geom` GEOMETRY NULL DEFAULT NULL,
    PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;

insert into geozones (name, geom) 
VALUES ('test', GeomFromText('Polygon((46 -123, 48 -123, 48 -121, 46 -121, 46 -123))') );

select name from geozones where Contains(geom, GeomFromText('POINT(47 -122)'));

If you are unable/unwilling to use either of the above, then a possible alternative (that I have not used) is this geoPHP project on GitHub. According to the Wiki it supports the contains operation which should do.

renick
  • 3,873
  • 2
  • 31
  • 40