问题

尝试写一个UDF,参数支持输入x,y与一个Polygon,返回结果是(x,y)是否在输入的Geometry之内?

环境

  1. eclipse
  2. odps 插件
  3. jts包:jts-1.8.jar

解法

import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import com.aliyun.odps.udf.UDF;
import com.vividsolutions.jts.geom.*;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader; /**
*
* @author xingxing.dxx
*
*/
public class WithinUDF extends UDF { private GeometryFactory geometryFactory = new GeometryFactory(); /*private Logger LOGGER = LoggerFactory.getLogger(WithinUDF.class);*/ /**
* 判断以x,y为坐标的点point(x,y)是否在geometry表示的Polygon中
* @param x
* @param y
* @param geometry wkt格式
* @return
*/
public Boolean evaluate(Double x, Double y, String geometry){ Coordinate coord = new Coordinate(x, y);
Point point = geometryFactory.createPoint(coord); WKTReader reader = new WKTReader(geometryFactory);
try {
Polygon polygon = (Polygon) reader.read(geometry);
return point.within(polygon);
} catch (ParseException e) {
/*LOGGER.error("geometry 格式错误",e);*/
throw new RuntimeException(e.getMessage());
}
} }

测试步骤

  1. 新建表
odpscmd
create table dual(x double,y double,geometry string);

    2.  准备数据(dual.txt),内容如下:

5;5;POLYGON((0 0, 10 0, 10 10, 0 10,0 0))

  3.  上传

tunnel u -fd ';' dual.txt project名称.dual; //列之间按照";"分割

  4.  运行

 true

  可以看到结果为true,执行成功

05-03 22:59