MySQL : how to find nearest polygon from a special point in mysql versions above 5.6.1? -


i've table urbanpolygons column holds polygon paths have point surrounded polygons , wanna search polygons , find 1 closer point.

the green box(polygon) 1 should chosen!

enter image description here

i manage point inside polygon or not:

select * urbanpolygons st_contains(urbanpolygons.paths, geomfromtext('point(29.632262, 52.497868)')); 

this question not duplicate of get polygons close lat,long in mysql or mysql find polygon nearest point

the older question mentioned above asked 6 years ago , plenty of geospatial features have been implemented in mysql time.

it's better answer considering new features of mysql < 5.6.1 offers,such st_contains , ...

sqlfiddle doesn't offer mysql version above 5.6 decided write litte example here:

create table `urbanpolygons` ( `id` int(11) not null auto_increment, `name` varchar(45) not null, `paths` polygon not null,  primary key (`id`),  unique key `id_unique` (`id`) ) insert urbanpolygons (`id`, `name`, `paths`) values ('1', 'area_1', st_geomfromtext('polygon ((29.63383 52.492869,29.63411 52.493942,29.634446 52.494586,29.634837 52.495658,29.635397 52.497289,29.635676 52.49804,29.635919 52.498856,29.636217 52.499778,29.63674 52.501345,29.636889 52.501967,29.637131 52.502847,29.636851 52.503405,29.636497 52.503748,29.635285 52.50495,29.634483 52.505808,29.632804 52.503641,29.631592 52.502053,29.629652 52.499628,29.628496 52.498126,29.629204 52.497783,29.629652 52.497482,29.630361 52.496774,29.631126 52.495594,29.632002 52.494242,29.632655 52.493577,29.633308 52.492912,29.63383 52.492869))')); insert urbanpolygons (`id`, `name`, `paths`) values ('2', 'area_2', st_geomfromtext('polygon ((29.633084 52.486968,29.632748 52.488384,29.629055 52.489929,29.625176 52.49023,29.625138 52.489328,29.624579 52.487869,29.624467 52.487311,29.627078 52.485809,29.628831 52.484179,29.630249 52.482634,29.631144 52.482419,29.633084 52.486968))')); insert urbanpolygons (`id`, `name`, `paths`) values ('3', 'area_3', st_geomfromtext('polygon ((29.622564 52.488553,29.624504 52.494046,29.626892 52.498853,29.630622 52.505204,29.632711 52.507436,29.628981 52.509153,29.624653 52.501085,29.620773 52.505376,29.616744 52.49645,29.617192 52.490956,29.622564 52.488553))')); 

i've point:

geomfromtext('point(29.630528, 52.492461)') 

this point out of provided polygons want nearest polygon point here area_1!

finally handle way:

fortunately st_distance() function provides distance geometry object others!(i believe distance between point , polygon calculated point nearest edge of polygons,tested google maps distance , provided distance st_distance)

select id     (select st_distance(paths,point(29.630528, 52.492461))as polydistance,id urbanpolygons)as nearestpoly     polydistance = (select min(st_distance(paths,point(29.630528, 52.492461))) urbanpolygons); 

which know bit complex , inefficient,and i'm eager learn better solution!


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -