I created some map (8000x8000) in photoshop. Every part of the map is created with custom shape tool and can be exported to *.ai file. So i exported one example shape to *.ai file, which has following contents:
%!PS-Adobe-2.0
%%Creator: Adobe Photoshop(TM) Pen Path Export 7.0
%%Title: (map-small-paddington.ai)
%%DocumentNeededResources: procset Adobe_packedarray 2.0 0
%%+ procset Adobe_IllustratorA_AI3 1.0 1
%%ColorUsage: Black&White
%%BoundingBox: 0 0 7998 7998
%%HiResBoundingBox: 0 0 7998.9998 7998.9998
%AI3_Cropmarks: 0 0 7998.9998 7998.9998
%%DocumentPreview: None
%%EndComments
%%BeginProlog
%%IncludeResource: procset Adobe_packedarray 2.0 0
Adobe_packedarray /initialize get exec
%%IncludeResource: procset Adobe_IllustratorA_AI3 1.0 1
%%EndProlog
%%BeginSetup
Adobe_IllustratorA_AI3 /initialize get exec
n
%%EndSetup
0.0 0.0 0.0 1.0 k
0 i 0 J 0 j 1 w 4 M []0 d
%%Note:
%Adobe_Photoshop_Path_Begin:<< /defaultFill false >>
*u
%AI3_Note:<< /operation /union >>
0 XR
4771.1153 4121.9960 m
4855.5878 4111.9732 4957.9582 4086.2029 5003.7736 4060.4311 c
5049.5891 4034.6599 5120.0382 3972.7994 5193.7738 3957.0505 c
5267.5089 3941.3016 5323.5226 3946.6539 5355.9339 3949.0778 C
5356.7559 3955.9301 5358.4651 3964.2284 5361.9409 3972.1987 C
5361.4717 3972.3317 L
5358.0990 3975.3640 5353.5658 3979.5329 5347.8721 3984.9663 c
5333.9035 3998.3003 5250.7315 4090.9969 Y
5162.4795 4165.2807 5134.5423 4193.2165 v
5106.6045 4221.1528 5064.0674 4221.7889 5022.1634 4227.5026 c
4980.2590 4233.2172 4968.8311 4237.6617 4932.0072 4254.1706 c
4895.1828 4270.6786 4843.1201 4292.2642 4827.8818 4298.6139 c
4812.6435 4304.9637 4796.1361 4316.3911 4778.9935 4340.5184 c
4761.8505 4364.6433 4745.9776 4385.5963 4728.8351 4398.9284 c
4723.2744 4403.2528 4716.9076 4407.1833 4712.3291 4410.3572 C
4681.6484 4314.8482 4661.9599 4219.4050 V
4662.8157 4217.2633 4684.8314 4162.2146 4701.6759 4132.7359 C
4710.9826 4122.7135 4771.1153 4121.9960 Y
n
*U
%Adobe_Photoshop_Path_End
%%Trailer
%%EOF
Now let's say I have two points: (4830,3780) which belongs to this shape, and (4480,3130) which doesn't. I know it because I checked it in Photoshop.
But my question is how to check every point I want programmatically? Is there a way to do it in PHP?
So question consists of two parts:
Any help would be appreciated.
The simplest way I can think of to check inside/outside is rasterizing the image and using color.
Create a version of your map where the shape elements are all black, and the background is white. Save that as a JPEG. If you have PHP installed with the GD or Imagemagik extensions, PHP can pick the color out of an image at the coordinates you want:
<?php
$im = imagecreatefromjpeg('map.jpg'); // Import black/white map image
var_dump(isInside($im, 4830, 3780)); // bool(true)
var_dump(isInside($im, 4480, 3130)); // bool(false)
function isInside($im, $x, $y) {
$rgb = imagecolorat($im, $x, $y);
$colors = imagecolorsforindex($im, $rgb);
$value = ($colors['red'] + $colors['green'] + $colors['blue'])/3; // Get average value
return ($value <= 10)? true : false; // If average value is less than 10, this pixel is black (or very close to it), so is inside a shape
}
?>
I'm using a $value <= 10
here rather than $value == 0
since at the edges of your shapes, you'll get a little aliasing where the white background will blend into the black shape. The color value is from zero to 255, so 10/255 is still pretty black, and will make sure that if you target a pixel that's near a shape border, it will still evaluate properly.