Java代码如何进行中国行政区划多边中生成随机点
发表于:2025-02-03 作者:千家信息网编辑
千家信息网最后更新 2025年02月03日,Java代码如何进行中国行政区划多边中生成随机点 ,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。因为要用到中国行政区划多边中生成随机
千家信息网最后更新 2025年02月03日Java代码如何进行中国行政区划多边中生成随机点
Java代码如何进行中国行政区划多边中生成随机点 ,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
因为要用到中国行政区划多边中生成随机点,在网上找了好长时间,发现也没有现成的东西,被迫自己写一下,代码为测试代码,大家对付看下吧。
用到相关的东西有:
1、echart 的中国行政区划的JSON文件
2、Google S2 Geometry Library Java版
3、基于Spring boot 开发
4、fastJson
话不多说,上代码
import com.alibaba.fastjson.JSONArray;import com.alibaba.fastjson.JSONObject;import com.google.common.geometry.*;import org.springframework.core.io.ClassPathResource;import java.io.IOException;import java.io.InputStream;import java.util.*;public class GEOJsonUtils { public static MapjsonList = new HashMap<>(); public static S2Polygon readJson(String code) throws IOException { if(jsonList.containsKey(code)){ }else{ String name = code.substring(0,4) + "00.json"; ClassPathResource classPathResource = new ClassPathResource("json/"+name); InputStream inputStream =classPathResource.getInputStream(); byte[] content = new byte[10240]; StringBuffer sb = new StringBuffer(); int len = 0; while ((len = inputStream.read(content)) > 0){ sb.append(new String(content,0, len)); } JSONObject parse = JSONObject.parseObject(sb.toString()); JSONArray features = parse.getJSONArray("features"); for (int i = 0 ; i < features.size(); i++) { JSONObject feature = (JSONObject)features.get(i); String id = feature.getString("id"); JSONObject geometry = feature.getJSONObject("geometry"); JSONArray encodeOffsets = geometry.getJSONArray("encodeOffsets"); JSONArray coordinates = geometry.getJSONArray("coordinates"); String type = geometry.getString("type"); if("Polygon".equals(type)){ List floats = decodePolygon(coordinates.getString(0), new double[]{encodeOffsets.getJSONArray(0).getDoubleValue(0), encodeOffsets.getJSONArray(0).getDoubleValue(1)}); List vertices = new ArrayList<>(); for (double[] item :floats) { vertices.add(S2LatLng.fromDegrees(item[1],item[0]).toPoint()); } S2Loop s2Loop = new S2Loop(vertices); //创建多边形 S2Polygon polygon = new S2Polygon(s2Loop); s2Loop.normalize(); jsonList.put(id,polygon); }else if("MultiPolygon".equals(type)){ List loops = new ArrayList<>(); for(int j = 0 ; j < coordinates.size() ; j++){ List floats = decodePolygon(coordinates.getString(j), new double[]{encodeOffsets.getJSONArray(j).getJSONArray(0).getDoubleValue(0), encodeOffsets.getJSONArray(j).getJSONArray(0).getDoubleValue(1)}); List vertices = new ArrayList<>(); for (double[] item :floats) { S2LatLng s2LatLng = S2LatLng.fromDegrees(item[1],item[0]); S2Point s2Point = s2LatLng.toPoint(); vertices.add(s2Point); } S2Loop s2Loop = new S2Loop(vertices); loops.add(s2Loop); s2Loop.normalize(); } //创建多边形 S2Polygon polygon = new S2Polygon(loops); jsonList.put(id,polygon); } } if(!jsonList.containsKey(code)){ jsonList.put(code, null); } } return jsonList.get(code); } public static S2LatLng randomPoint(S2Polygon polygon){ for(int i = 0 ; i < polygon.numLoops() ; i++){ S2LatLngRect rect = polygon.loop(i).getRectBound(); S2LatLng s2LatLng1 = new S2LatLng(rect.lo().toPoint()); S2LatLng s2LatLng2 = new S2LatLng(rect.hi().toPoint()); double minX = 0; double minY = 0; double maxX = 0; double maxY = 0; if(s2LatLng1.lat().degrees() > s2LatLng2.lat().degrees()){ maxX = s2LatLng1.lat().degrees(); minX = s2LatLng2.lat().degrees(); }else{ maxX = s2LatLng2.lat().degrees(); minX = s2LatLng1.lat().degrees(); } if(s2LatLng1.lng().degrees() > s2LatLng2.lng().degrees()){ maxY = s2LatLng1.lng().degrees(); minY = s2LatLng2.lng().degrees(); }else{ maxY = s2LatLng2.lng().degrees(); minY = s2LatLng1.lng().degrees(); } Random r = new Random(); S2Point rPoint = new S2Point(); for(int j = 0 ; j < 10 ; j++){ double x = r.nextDouble()*(maxX - minX) + minX; double y = r.nextDouble()*(maxY - minY) + minY; rPoint = S2LatLng.fromDegrees(x, y).toPoint(); if(polygon.contains(rPoint)){ S2LatLng s2LatLng = new S2LatLng(rPoint); return s2LatLng; } } if(i == polygon.numLoops() - 1){ i = 0; } } return null; } public static List decodePolygon(String coordinate, double[] encodeOffsets){ List result = new ArrayList<>(); double prevX = encodeOffsets[0]; double prevY = encodeOffsets[1]; for (int i = 0; i < coordinate.length(); i += 2) { int x = coordinate.charAt(i) - 64; int y = coordinate.charAt(i + 1) - 64; x = (x >> 1) ^ (-(x & 1)); y = (y >> 1) ^ (-(y & 1)); x += prevX; y += prevY; prevX = x; prevY = y; result.add(new double[]{x / 1024d, y / 1024d}); } return result; }}
关于Java代码如何进行中国行政区划多边中生成随机点 问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。
代码
行政区划
中国
中生
问题
东西
多边形
更多
j++
帮助
解答
易行
简单易行
上代
内容
小伙
小伙伴
文件
方法
知识
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
网络安全产业最新研究
云南项目软件开发服务商
qcma升级固件数据库刷新
数据库的创建实验分析
数据库中用户头像表
网络安全问题的研究学习
电视盒怎么改成服务器
平板电脑输入法软件开发
网络安全工作团队
广播站网络技术部
网络安全法三个同步
直接登录服务器软件
网络安全注意事项英文演讲稿
种子的服务器
vfp数据库设计报告
校园安全及网络安全心得体会
网络安全云大赛
介绍市面上主流的云服务器
大企业网站用自己服务器吗
请简述一下对网络安全的认识
软件开发用户代表
数据库系统概括第五版电子版
app软件开发大概几钱
代表高科技互联网的生肖
徐州上山小区网络安全吗
计算机网络安全有哪些特征
excle以模板导入数据库
高校网络安全微视频大赛
系统软件开发方式
网络安全架构规范