四、在全部的六边形区域内过滤出合适的区域作为虚拟车站点
实现方式:
对历史库的订单数据进行汇总,选出某个区域的订单数大于某个阈值(比如100)
代码实现:
//1.创建h3实例 val h3 = H3Core.newInstance //2.经纬度转换成hash值 def locationToH3(lat: Double, lon: Double, res: Int): Long = { h3.geoToH3(lat, lon, res) } //将h3注册为udf函数 sparkSession.udf.register("locationToH3", locationToH3 _) 4.//在sql语句中使用h3接口进行六边形栅格化 val gridDf = sparkSession.sql( s""" |select |ORDER_ID, |CITY_ID, |STARTING_LNG, |STARTING_LAT, |locationToH3(STARTING_LAT,STARTING_LNG,12) as h3code | from order |""".stripMargin ) //5.分组统计 得到大于100个订单的区域 val groupCountDf = gridDf.groupBy("h3code").count().filter("count>=100") //统计结果注册临时视图 groupCountDf.createOrReplaceTempView("groupcount") //6.使用过滤后的区域与原始所有订单表进行join操作,升序取出最小精度,最小维度的点作为虚拟车站的经纬度位置信息,得到虚拟车站 val groupJoinDf = sparkSession.sql( s""" |select |ORDER_ID, |CITY_ID, |STARTING_LNG, |STARTING_LAT, |row_number() over(partition by order_grid.h3code order by STARTING_LNG,STARTING_LAT asc) rn | from order_grid join groupcount on order_grid.h3code = groupcount.h3code |having(rn=1) |""".stripMargin)
五、求出某地市的某个区有多少个虚拟车站
如何实现:
1.查询出地市区边界的ip集合
借助高德或者自己公司的地图api来
https://lbs.amap.com/api/webservice/guide/api/district
2.借助空间几何WKT工具判断现有的经纬度点位是否该地区内
WKT 链接: https://blog.csdn.net/Claire_ll/article/details/84952339
判断一个点是不是在多边形内 实例代码:
String wktPoly = "POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))"; //请自行搜素了解wkt格式 String wktPoint = "POINT (30 30)"; WKTReader reader = new WKTReader(JTSFactoryFinder.getGeometryFactory()); GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null); Geometry point = reader.read(wktPoint); Geometry poly = reader.read(wktPoly); poly.contains(point); //返回true或false
//获取雨花台区的边界ip集合 List<District> districtList = new ArrayList<District>(); JSONArray districts = MapUtil.getDistricts("雨花台区", null); MapUtil.parseDistrictInfo(districts, null, districtList); GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null); WKTReader reader = new WKTReader(geometryFactory); // 用南京所有区的所有边界值经纬 构建多个多边形区域 List<Geometry> polygons = districtList.stream().map(district -> { String wktPolygon = "POLYGON((" + district.getPolygon().replaceAll(",", " ").replaceAll(";", ",") + "))"; //得到多边形对象 Geometry polygon = null; try { polygon = reader.read(wktPolygon); } catch (ParseException e) { e.printStackTrace(); } return polygon; }).collect(Collectors.toList()); //使用 经纬度构建点 /** * 江苏省 南京市 雨花台区 * * 经度:118.77 * 纬度:32.00 */ //测试这个ip在不在雨花台区 String wktPoint = "POINT(118.77 32.00)"; // Point point = (Point) reader.read(wktPoint); //判断多边形是否包含点 雨花台区是否包含ip 118.772222 32.779990 polygons.forEach(polygon -> System.out.println(polygon.contains(point)));
3.经纬度存在某个地区,则把该虚拟车站经纬度与城市名保存到hbase或者数据库中,供前端进行点位展示