MySQL gisは、ある点がポリゴンにあるかどうかを判断します.
5202 ワード
詳細
!!!!!! 点が多角形の最小矩形の中の点を取得します.Javaを加えて2回目の判断を行います.
テーブルの作成:
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);
——————————————————————————————
挿入ポイント:
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は、ある点がポリゴン内にあるかどうかを判断します.
!!!!!! 点が多角形の最小矩形の中の点を取得します.Javaを加えて2回目の判断を行います.
テーブルの作成:
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);
——————————————————————————————
挿入ポイント:
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,List dlist){
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));*/
}
}