!!!!!! 取得点为 多边形的最小矩形里面的点。加入java进行第二次判断。
建表:
CREATE TABLE `test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`pnt` point DEFAULT NULL,
`pgn` polygon DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=18 DEFAULT CHARSET=utf8;
插入数据:
INSERT INTO `test` VALUES ('12', GeomFromText('POINT(1 1)'), null);
INSERT INTO `test` VALUES ('11', GeomFromText('POINT(1 1)'), null);
INSERT INTO `test` VALUES ('1', GeomFromText('POINT(1 1)'), null);
INSERT INTO `test` VALUES ('2', GeomFromText('POINT(1 5)'), null);
INSERT INTO `test` VALUES ('3', GeomFromText('POINT(2 2)'), null);
INSERT INTO `test` VALUES ('4', GeomFromText('POINT(2 3)'), null);
INSERT INTO `test` VALUES ('5', GeomFromText('POINT(3 2)'), null);
INSERT INTO `test` VALUES ('8', GeomFromText('POINT(2 3)'), null);
INSERT INTO `test` VALUES ('7', GeomFromText('POINT(2 2)'), null);
INSERT INTO `test` VALUES ('9', GeomFromText('POINT(12 12)'), null);
INSERT INTO `test` VALUES ('10', null, null);
INSERT INTO `test` VALUES ('13', null, GeomFromText('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0), (5 5, 7 5, 7 7, 5 7, 5 5))'));
INSERT INTO `test` VALUES ('14', null, GeomFromText('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))'));
INSERT INTO `test` VALUES ('15', null, GeomFromText('POINT(4 4)'));
INSERT INTO `test` VALUES ('16', GeomFromText('POINT(4 4)'), null);
INSERT INTO `test` VALUES ('17', GeomFromText('POINT(4 4)'), null);
——————————————————————————————
插入点point:
INSERT INTO test VALUE ('17',GeomFromText('POINT(4 4)'),NULL);
插入点多边形:
INSERT INTO test VALUE ('14',NULL,GeomFromText('POLYGON((0 0,10 0,10 10,0 10,0 0))'));
判断点在哪个多边形内:
select id from test where MBRContains(pgn, GeomFromText('Point(1 1)'));
Java 判断某个点是否在多边形内。
package com.fjxhx.business.dida.action; import java.awt.geom.Point2D; import java.util.ArrayList; import java.util.List; import com.fjxhx.business.dida.model.NetgridModel; import com.fjxhx.business.dida.model.OrderModel; import com.fjxhx.business.system.action.BaseAction; public class TestGis extends BaseAction{ /** * 判断当前位置是否在围栏内 */ //dlist为某个多变形的所有点 public boolean isInPolygon(OrderModel order,Listdlist){ double p_x =Double.parseDouble(order.getLocationX()); double p_y =Double.parseDouble(order.getLocationY()); Point2D.Double point = new Point2D.Double(p_x, p_y); List pointList= new ArrayList (); for (NetgridModel enclosure : dlist){ double polygonPoint_x=Double.parseDouble(enclosure.getLonX()); double polygonPoint_y=Double.parseDouble(enclosure.getLatY()); Point2D.Double polygonPoint = new Point2D.Double(polygonPoint_x,polygonPoint_y); pointList.add(polygonPoint); } TestGis test = new TestGis(); return test.checkWithJdkGeneralPath(point,pointList); } /** * 返回一个点是否在一个多边形区域内 * @param point * @param polygon * @return */ private boolean checkWithJdkGeneralPath(Point2D.Double point, List polygon) { java.awt.geom.GeneralPath p = new java.awt.geom.GeneralPath(); Point2D.Double first = polygon.get(0); p.moveTo(first.x, first.y); polygon.remove(0); for (Point2D.Double d : polygon) { p.lineTo(d.x, d.y); } p.lineTo(first.x, first.y); p.closePath(); return p.contains(point); } public static void main(String[] args) { /*OrderModel order=new OrderModel(); order.setLocationX("3"); order.setLocationY("1.5"); List nlist = new ArrayList (); NetgridModel ne =new NetgridModel(); NetgridModel ne1 =new NetgridModel(); NetgridModel ne2=new NetgridModel(); NetgridModel ne3=new NetgridModel(); ne.setLonX("2"); ne.setLatY("0"); nlist.add(0, ne); ne1.setLonX("0"); ne1.setLatY("2"); nlist.add(1, ne1); ne2.setLonX("2"); ne2.setLatY("4"); nlist.add(2, ne2); ne3.setLonX("4"); ne3.setLatY("2"); nlist.add(3, ne3); TestGis test = new TestGis(); System.out.println(test.isInPolygon(order, nlist));*/ } }