问题

  • 根据给定的gps点point(x,y)和北京的shape数据,关联出 AOI ID

IO

  • 输入

    • gps点表
    • create table gps
      (
         x double, //经度
         y double //纬度
      )
      

      +------------+------------+
      | x | y |
      +------------+------------+
      | 113.570517 | 22.149751 |
      | 113.57431 | 22.152167 |
      | 113.544971 | 22.190477 |
      | 113.546035 | 22.203194 |
      | 113.568557 | 22.168465 |

    • shape文件
    • ogrinfo -ro -al beijing_20160122152124568.shp
      
      OGRFeature(beijing_20160122152124568):
        AOIID (String) = G00AOI000001ZIQ3
        POIID (String) = B0FFFTLAU4
        Name (String) = ˮѧԺԺ
        Type (String) =
        Disp_Class (Integer) =
        Reference (String) = (null)
        ParentID (String) = (null)
        CenterX (Real) = 116.322184
        CenterY (Real) = 39.931526
        Area (Real) = 19187.288500
        CreateTime (String) = -- :
        UpdateTime (String) = -- :
        Update (String) = u
        CityName (String) =   Memo (String) =   POLYGON ((418756.3848 143755.6608,418756.5396 143755.7688,418759.47 143755.7688,418760.8884 143755.7616,418760.8884 143754.616799999989,418761.9324 143754.5628,418763.556 143754.5376,418763.624399999972 143753.8716,418763.9304 143753.810399999988,418763.898 143751.5064,418756.395599999989 143751.733199999988,418756.395599999989 143753.263199999987,418756.3848 143755.6608))
  • 输出  
    • 根据gps表中点,判断是否内含与shape文件的polygon中,关联出AOIID
    • 输出表
    • +------------+------------+--------+
      | x          | y          | aoi_id |
      +------------+------------+--------+
      | 419217.2568 | 143807.9436 | NULL   |
      | 418659.14879999997 | 143630.046 | NULL   |
      | 418659.6312 | 143629.9848 | NULL   |
      | 418024.2636 | 143054.75519999999 | NULL   |
      | 419875.88399999996 | 143947.60199999998 | NULL   |
      | 418042.9692 | 143037.2556 | NULL   |
      | 418469.9436 | 144192.348 | NULL   |
      | 418469.9436 | 144192.348 | G00AOI000001WBC2 |
      | 420292.3248 | 143810.84879999998 | NULL   |
      | 418509.26279999997 | 143762.994 | NULL   |

知识点

  • GDAL/ogr2ogr使用
  • JTS geometry内含判断
  • JTS Rtree使用

数据准备

  • shape文件

    • 提取wkt和aoiid字段
    • ogr2ogr -lco "GEOMETRY=AS_WKT" -lco "SEPARATOR=SEMICOLON" -f CSV beijing_20160122152124568.csv -sql "select AOIID from beijing_20160122152124568" beijing_20160122152124568.shp 
    • 去掉文件中的双引号
    • sed -e '/"/s/"//g' beijing_20160122152124568.csv > beijing.csv
    • odps中创建表
    • create table beijing(wkt string,lua_id bigint);
    • 上传文件
    • tunnel u -fd ';' beijing.csv.csv autonavi_tinfo_dev.exercise_land_user_area; //列之间按照";"分割 
    • 将表作为资源文件加入odps
    • add table beijing

编码

  • udtf
  • import java.util.Iterator;
    import java.util.List;
    
    import com.aliyun.odps.udf.ExecutionContext;
    import com.aliyun.odps.udf.UDFException;
    import com.aliyun.odps.udf.UDTF;
    import com.aliyun.odps.udf.annotation.Resolve;
    import com.vividsolutions.jts.geom.Coordinate;
    import com.vividsolutions.jts.geom.Envelope;
    import com.vividsolutions.jts.geom.GeometryFactory;
    import com.vividsolutions.jts.geom.Point;
    import com.vividsolutions.jts.geom.Polygon;
    import com.vividsolutions.jts.index.strtree.STRtree;
    import com.vividsolutions.jts.io.WKTReader;
    
    /**
     * 加载table中的资源,字段如: wkt string ,bigint lua_id 参数: x,y,tableName,idxWkt,idxId
     *
     * @author xingxing.dxx double,double,string,bigint,bigint->double,double,string
     *
     */
    @Resolve({ "double,double->double,double,string" })
    public class ExerciseWithinUDTF extends UDTF {
    
        /*
         * private Logger LOGGER =
         * LoggerFactory.getLogger(ExerciseWithinUDTF.class);
         */
    
        private GeometryFactory geometryFactory = new GeometryFactory();
        private WKTReader reader = new WKTReader(geometryFactory);
        private STRtree strtree;
        private ExecutionContext ctx;
    
        public void setup(ExecutionContext ctx) throws UDFException {
            this.ctx = ctx;
        }
    
        @Override
        public void process(Object[] args) throws UDFException {
    
            final double x = (double) args[0];
            final double y = (double) args[1];
            String tableName = (String) args[2];
            int idxWkt = ((Long) args[3]).intValue();
            int idxId = ((Long) args[4]).intValue();
    
            // 1.初始化,第一次才加载表资源
            if (strtree == null) {
                strtree = new STRtree();
                try {
                    Iterator<Object[]> iterator = this.ctx.readResourceTable(tableName).iterator();
                    while (iterator.hasNext()) {
                        Object[] o = iterator.next();
                        String wkt = (String) o[idxWkt];
                        String id = (String) o[idxId];
                        Polygon polygon = (Polygon) reader.read(wkt);
                        GeometryBean gb = new GeometryBean(polygon, id);
                        strtree.insert(polygon.getEnvelopeInternal(), gb);
                    }
    
                } catch (Exception e) {
                    /* LOGGER.error("资源加载出现问题",e); */
                    e.printStackTrace();
                    throw new RuntimeException(e.getMessage());
    
                }
            }
    
            Coordinate coord = new Coordinate(x, y);
            Point point = geometryFactory.createPoint(coord);
            Envelope envelope = new Envelope(coord);
            List<GeometryBean> items = strtree.query(envelope);
            String luaId = null;
            if (items != null && items.size() > 0) {
                for (Object o : items) {
                    GeometryBean geometryBean = (GeometryBean) o;
                    if (point.within(geometryBean.getPolygon())) {
                        luaId = geometryBean.getId();
                    }
                    forward(x, y, luaId);
                }
            } else {
                forward(x, y, luaId);
            }
    
        }
    
        public void close() throws UDFException {
    
        }
    
    }
  • sql
  • CREATE TABLE result_gps_within
    AS
    SELECT
        udtf_exercise_within(x,y,,)
        as(x,y,aoi_id)
       FROM gps 
  • SELECT a.aoi_id,count(distinct x,y) num
      FROM
         (
          SELECT
              udtf_exercise_within(x*,y*,,)
              as(x,y,aoi_id)
            FROM gps
         ) a where aoi_id is not null
     GROUP BY a.aoi_id 

碰到的坑

  • shape文件是秒的格式,需要转换成度
  • ogr2ogr转成的csv文件需要去掉双引号,否则不能反序列化为polygon
  • udtf读取资源文件需要把表当做资源添加

UDF2的更多相关文章

  1. DB2 嵌入式应用中定义游标(开放平台上)

    DECLARE CURSOR statement The DECLARE CURSOR statement defines a cursor. Invocation Although an inter ...

  2. AnonymousType匿名类型和对象之间的转换

    本文转载:http://www.cnblogs.com/dean-Wei/p/3150553.html 一.匿名对象转换为对象. 1.问题: 2.解决方案:强制指定类型. 解决之. 二. 对象转换为匿 ...

  3. mysql常用的提权方法

    一,利用MOF提权 Windows 管理规范 (WMI) 提供了以下三种方法编译到 WMI 存储库的托管对象格式 (MOF) 文件: 方法 1: 运行 MOF 文件指定为命令行参数将 Mofcomp. ...

  4. 【Spark篇】---SparkSql之UDF函数和UDAF函数

    一.前述 SparkSql中自定义函数包括UDF和UDAF UDF:一进一出  UDAF:多进一出 (联想Sum函数) 二.UDF函数 UDF:用户自定义函数,user defined functio ...

  5. Spark SQL UDF示例

    UDF即用户自定函数,注册之后,在sql语句中使用. 基于scala-sdk-2.10.7,Spark2.0.0. package UDF_UDAF import java.util import o ...

  6. 【Spark篇】---SparkSQL中自定义UDF和UDAF,开窗函数的应用

    一.前述 SparkSQL中的UDF相当于是1进1出,UDAF相当于是多进一出,类似于聚合函数. 开窗函数一般分组取topn时常用. 二.UDF和UDAF函数 1.UDF函数 java代码: Spar ...

  7. 【Spark-SQL学习之三】 UDF、UDAF、开窗函数

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk1.8 scala-2.10.4(依赖jdk1.8) spark ...

  8. Windows XP UDF 2.5 补丁,播放蓝光ISO光盘必备

    蓝光光盘的文件系统是UDF2.5,Windows XP及以下的操作系统默认不能支持这个文件系统.当我们在XP系统中使用蓝光光盘或蓝光ISO文件时,就会提示“Windows不能从此盘读取,此盘可能已损坏 ...

  9. Kafka:ZK+Kafka+Spark Streaming集群环境搭建(十五)Spark编写UDF、UDAF、Agg函数

    Spark Sql提供了丰富的内置函数让开发者来使用,但实际开发业务场景可能很复杂,内置函数不能够满足业务需求,因此spark sql提供了可扩展的内置函数. UDF:是普通函数,输入一个或多个参数, ...

随机推荐

  1. char str[]和char *str的区别

    1.http://blog.csdn.net/szchtx/article/details/10396149 char ss[]="C++";  ss[0]='c';        ...

  2. coreseek 提示 client version is higher than daemon version 解决办法

    安装好coreseek,开启了服务之后,通过 sphinx php扩展去请求数据,提示:client version is higher than daemon version (client is ...

  3. C# 128位AES 加密解密 (转)

    /// AES加密 2         /// </summary> 3         /// <param name="inputdata">输入的数据 ...

  4. STM32 程序所占用空间计算 && FLASH存储的起始地址计算

    程序编译完成,会乘车program size .. 对STM32容量选型或者 计算FLASH 充当EEPROM起始地址时会用到此参数. 按照下面截图  程序空间 = (16700+732+4580)/ ...

  5. 调皮的MySQL服务 请你不要再躲猫猫

    问题说明 Win10企业版 64位: MySQL5.6 64位: 临时在本机部署一套系统,需要用到MySQL,发现Windows服务下居然找不到MySQL这个服务,原先配置好的GUI以及应用程序都连不 ...

  6. js判断手机浏览器并跳转到手机网站

    function uaredirect(murl){ try { if(document.getElementById("bdmark") != null){ return; } ...

  7. css3,background-clip/background-origin的使用场景,通俗讲解

    先不说background-clip/background-origin的用法,我们先来聊聊css背景方面的知识. <!DOCTYPE html> <html lang=" ...

  8. 固态硬盘寿命实测让你直观SSD寿命!--转

    近年来,高端笔记本及系列上网本越来越多的采用固态硬盘来提升整机性能,尽管众所周知固态硬盘除 了在正常的使用中带来更快速度的体验外,还具有零噪音.不怕震动.低功耗等优点,但大家对固态硬盘的寿命问题的担忧 ...

  9. codeforce No to Palindromes!(枚举)

    /* 题意:给定一个字符串中没有任何长度>1的回文子串!求按照字典序的该串的下一个字符串 也不包含长度>1的任何回文子串! 思路:从最低位进行枚举,保证第i位 不与 第 i-1位和第 i- ...

  10. weblogic集群中获取jndi的方式

    # The following example specifies a list of WebLogic Servers using the same port: ht.put(Context.PRO ...