Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate Random points in the multilateral Administrative Division of China by Java Code

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to generate random points in the multilateral administrative division of China with Java code? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Because I want to use the multilateral administrative division of China to generate random points, I have been looking for it on the Internet for a long time, and found that there is nothing ready-made, so I am forced to write it myself. The code is a test code, so let's deal with it.

The related things used are:

1. The JSON document of echart's administrative division in China.

2. Google S2 Geometry Library Java version

3. Development based on Spring boot

4 、 fastJson

Don't say much, put on the code.

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 jsonList = new HashMap () Public static S2Polygon readJson (String code) throws IOException {if (jsonList.containsKey (code)) {} else {String name = code.substring (0P4) + "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 } this is the answer to the question about how the Java code can be used to generate random points in the multilateral administrative division of China. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 282

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report