您好,登錄后才能下訂單哦!
Java代碼如何進(jìn)行中國(guó)行政區(qū)劃多邊中生成隨機(jī)點(diǎn) ,針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
因?yàn)橐玫街袊?guó)行政區(qū)劃多邊中生成隨機(jī)點(diǎn),在網(wǎng)上找了好長(zhǎng)時(shí)間,發(fā)現(xiàn)也沒有現(xiàn)成的東西,被迫自己寫一下,代碼為測(cè)試代碼,大家對(duì)付看下吧。
用到相關(guān)的東西有:
1、echart 的中國(guó)行政區(qū)劃的JSON文件
2、Google S2 Geometry Library Java版
3、基于Spring boot 開發(fā)
4、fastJson
話不多說(shuō),上代碼
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 Map<String, S2Polygon> jsonList = 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<double[]> floats = decodePolygon(coordinates.getString(0), new double[]{encodeOffsets.getJSONArray(0).getDoubleValue(0), encodeOffsets.getJSONArray(0).getDoubleValue(1)}); List<S2Point> vertices = new ArrayList<>(); for (double[] item :floats) { vertices.add(S2LatLng.fromDegrees(item[1],item[0]).toPoint()); } S2Loop s2Loop = new S2Loop(vertices); //創(chuàng)建多邊形 S2Polygon polygon = new S2Polygon(s2Loop); s2Loop.normalize(); jsonList.put(id,polygon); }else if("MultiPolygon".equals(type)){ List<S2Loop> loops = new ArrayList<>(); for(int j = 0 ; j < coordinates.size() ; j++){ List<double[]> floats = decodePolygon(coordinates.getString(j), new double[]{encodeOffsets.getJSONArray(j).getJSONArray(0).getDoubleValue(0), encodeOffsets.getJSONArray(j).getJSONArray(0).getDoubleValue(1)}); List<S2Point> 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(); } //創(chuàng)建多邊形 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<double[]> decodePolygon(String coordinate, double[] encodeOffsets){ List<double[]> 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; } }
關(guān)于Java代碼如何進(jìn)行中國(guó)行政區(qū)劃多邊中生成隨機(jī)點(diǎn) 問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。