java实现将经纬度json存入数据库

需求:将以下字符串,转化为经度、纬度、高度 存入数据库
 

String coordinateString = "[[118.8353364,28.3286737,1120.04],[118.835408,28.3287202,1120.04],[118.8355049,28.3287792,1123.35],[118.835613,28.3287881,1125.54],[118.8357236,28.3288305,1127.52],[118.8358145,28.3288979,1129.19],[118.8359333,28.3289213,1130.11],[118.8360404,28.3289477,1130.51],[118.8361293,28.3290142,1130.8],[118.8361977,28.3290906,1131],[118.8362714,28.3291599,1130.92],[118.8363573,28.3292142,1130.26],[118.8364559,28.3292526,1129.13],[118.8365623,28.3292704,1128.52],[118.8366449,28.329327,1128.11]]";
//string转化为数组
private static double[][] parseCoordinates(String coordinateString) {
		// 去除外层的中括号,并将字符串拆分为经纬度点数组
		String[] points = coordinateString.replaceAll("\\[\\[|\\]\\]", "").split("\\],\\[");

		// 初始化经纬度和高度数组
		double[][] coordinates = new double[points.length][3];

		for (int i = 0; i < points.length; i++) {
			// 拆分每个子字符串为经度、纬度和高度(如果存在)
			String[] pointData = points[i].split(",");

			// 确保每个点都有三个值
			if (pointData.length != 3) {
				throw new IllegalArgumentException("每个点必须包含三个值");
			}

			// 将经纬度和高度值转换为双精度浮点数
			coordinates[i][0] = Double.parseDouble(pointData[0]);
			coordinates[i][1] = Double.parseDouble(pointData[1]);
			coordinates[i][2] = Double.parseDouble(pointData[2]);
		}

		return coordinates;
	}

public void saveLine(String aa, Long bb) throws InterruptedException {
		aa = "[[118.8353364,28.3286737,1120.04],[118.835408,28.3287202,1120.04],[118.8355049,28.3287792,1123.35],[118.835613,28.3287881,1125.54],[118.8357236,28.3288305,1127.52],[118.8358145,28.3288979,1129.19],[118.8359333,28.3289213,1130.11],[118.8360404,28.3289477,1130.51],[118.8361293,28.3290142,1130.8],[118.8361977,28.3290906,1131],[118.8362714,28.3291599,1130.92],[118.8363573,28.3292142,1130.26],[118.8364559,28.3292526,1129.13],[118.8365623,28.3292704,1128.52],[118.8366449,28.329327,1128.11],[118.8366018,28.3293882,1129.05],[118.8364897,28.3294316,1130.44],[118.8363858,28.3294918,1132.72],[118.8363204,28.3295772,1134.6],[118.8363469,28.3296718,1135.86],[118.8363828,28.3297668,1136.44],[118.8363469,28.3298504,1136.66],[118.8362523,28.3298585,1137.47],[118.8361627,28.3299175,1139.81],[118.8360924,28.3299913,1141.07],[118.836018,28.3300767,1141.61],[118.835989,28.3301686,1141.29],[118.8359676,28.3302656,1141.13],[118.8359057,28.3303349,1141.64],[118.8358303,28.3304046,1142.33],[118.8357207,28.3304221,1141.79],[118.8356099,28.3304425,1142.4],[118.8354933,28.3304581,1143.28],[118.8353852,28.3304676,1145.23],[118.8352946,28.330513,1146.72],[118.8351919,28.330533,1148.61],[118.8350913,28.3305192,1150.49],[118.8349843,28.3305237,1151.87],[118.8348957,28.3305652,1152.79],[118.8349268,28.3306428,1153.08],[118.8350177,28.3307145,1152.94],[118.8351031,28.3307904,1152.55],[118.8352097,28.330874,1152.13],[118.8353012,28.330946,1152.41],[118.8353891,28.3310083,1151.78],[118.8355098,28.3310353,1150.33],[118.8356112,28.3310744,1150.26],[118.8356753,28.3311502,1150.95],[118.8357426,28.3312404,1151.84],[118.8358089,28.331327,1151.62],[118.8358173,28.331423,1151.92],[118.8358227,28.3315219,1152.06],[118.83588,28.3315921,1151.7],[118.8359331,28.3316818,1151.35],[118.8359786,28.3317694,1150.27],[118.836037,28.3318516,1149.59],[118.8360539,28.3319391,1147.98],[118.8360648,28.3320344,1145.8],[118.8360827,28.3321265,1142.95],[118.8361105,28.3322196,1140.38],[118.8362163,28.3322671,1131.05],[118.8363182,28.3323162,1126.61],[118.8364148,28.3323704,1124.75],[118.8364786,28.3324435,1124.8],[118.8365304,28.3325259,1124.75],[118.8365559,28.3326212,1123.55],[118.8365716,28.3327216,1122.65],[118.8366223,28.3328117,1122.29],[118.8366253,28.3329059,1121.74],[118.8365665,28.3329795,1121.66],[118.8364562,28.3330279,1120.77],[118.8364434,28.3331174,1122.36],[118.836433,28.3332119,1122.5],[118.836433,28.3333096,1119.01],[118.8363571,28.3333443,1114.89],[118.8362305,28.3333355,1109.85],[118.8361297,28.3333628,1103.2],[118.836179,28.3334632,1096.55],[118.8362385,28.3335475,1090.71],[118.836292,28.333636,1087.12],[118.836282,28.3337231,1084.03],[118.8362134,28.3337989,1081.43],[118.8361102,28.3338429,1079.2],[118.8359971,28.3338641,1077.43],[118.8358881,28.3339016,1076.56],[118.8357835,28.333961,1076.53],[118.8357065,28.3340368,1075.87],[118.8356312,28.3341049,1075.75],[118.8355712,28.3340597,1074.84],[118.8355342,28.3341085,1073.55],[118.8354569,28.3341779,1072.11],[118.8353493,28.3341805,1069.52],[118.8352782,28.3341117,1068.03],[118.8351864,28.334066,1065.98],[118.8350832,28.3340648,1063.88],[118.8350329,28.3341484,1061.64],[118.834983,28.3342399,1059.18],[118.8348925,28.3342517,1056.38],[118.8347846,28.3342206,1055.2],[118.8346661,28.3341996,1052.96],[118.8345609,28.3342182,1050.62],[118.834467,28.3342752,1048.48],[118.8343903,28.3343534,1046.94],[118.8343045,28.3344216,1045.94],[118.8341955,28.3344562,1044.39],[118.8340896,28.3344871,1042.02],[118.8340851,28.334558,1038.2],[118.8341748,28.3346034,1034.82],[118.834231,28.3346862,1032.02],[118.8342843,28.3347725,1030.11],[118.8343625,28.3348382,1029.47],[118.8343611,28.3349193,1027.63],[118.8344171,28.3349928,1020.55],[118.8345168,28.3350415,1014.59],[118.8346141,28.3350926,1009.74],[118.8346783,28.3351644,1006.44],[118.8347559,28.3352312,1003.76],[118.8348824,28.3352788,1001.74],[118.8349985,28.3353379,999.22],[118.8350925,28.3353984,996.71],[118.8351479,28.3354796,993.55],[118.8352002,28.3355718,991.52],[118.8352398,28.3356633,990.46],[118.8352398,28.3356633,990.46],[118.835168,28.3357388,990.37],[118.835168,28.3357388,990.37],[118.8350646,28.3357346,987.31],[118.8349669,28.3357042,986.14],[118.8348773,28.3356434,987.07],[118.8347734,28.3355974,988],[118.8346692,28.3355608,989.13],[118.8345449,28.3355481,990.34],[118.8344347,28.335538,992.34],[118.8343299,28.3355423,996.65],[118.8342248,28.3355517,999.2],[118.8341167,28.3355398,1000.63],[118.8340082,28.3355462,1001.67],[118.8338986,28.3355549,1002.14],[118.8337848,28.3355695,1003.32],[118.8336674,28.3355814,1004.81],[118.8335744,28.3356312,1005.76],[118.833513,28.3357117,1007.42],[118.8334435,28.3357947,1009.72],[118.8333434,28.335856,1010.93],[118.8332398,28.3358949,1012.45],[118.8331289,28.3359138,1015.48],[118.8330425,28.3359662,1017.5],[118.8330111,28.3360529,1020.27],[118.8330404,28.3361445,1020.86],[118.8331173,28.3362389,1022.13],[118.8331939,28.3363236,1023.86],[118.833276,28.3364123,1026.39],[118.8333634,28.3364911,1027.49],[118.8334437,28.3365908,1026.61],[118.8334562,28.3366931,1025.85],[118.8333754,28.336747,1025.86],[118.8332626,28.3367448,1025.89],[118.8331371,28.3367219,1027.49],[118.8330354,28.3367498,1029.63],[118.8329193,28.33677,1029.72],[118.83283,28.3367179,1034.33],[118.8327429,28.3366559,1035.43],[118.8326235,28.3366382,1035.06],[118.8325198,28.336669,1026.32],[118.832481,28.3366085,1025.83],[118.8324753,28.336513,1028.29],[118.8324491,28.3364199,1029.94],[118.83238,28.3363463,1032.11],[118.8323255,28.3362549,1031.93],[118.8322829,28.3361648,1031.13],[118.8322566,28.3360655,1029.03],[118.8322488,28.3359569,1030.87],[118.8322043,28.3358729,1029.55],[118.8321535,28.3357859,1027.2],[118.8321013,28.335702,1025.26],[118.8320722,28.3356153,1024.56],[118.83208,28.3355207,1024.71],[118.8320751,28.3354285,1026.72],[118.8320176,28.3353598,1027.89],[118.8319066,28.3353415,1029.07],[118.831786,28.335345,1028.85],[118.8317122,28.3352929,1028.66],[118.8317002,28.3352075,1029.59],[118.8317039,28.3351069,1030.82],[118.8316612,28.3350236,1031.81],[118.8315712,28.3349684,1031.51],[118.8314594,28.3349673,1030.94],[118.8313435,28.334952,1030.16],[118.8312804,28.334884,1029.91],[118.8312016,28.334811,1029.34],[118.8311038,28.3347553,1028.48],[118.8310991,28.3346732,1027.61],[118.831144,28.3345823,1026.63],[118.8311749,28.3344847,1025.03],[118.8311431,28.3343956,1022.41],[118.8310753,28.3343164,1019.79],[118.8310179,28.3342337,1017.83],[118.8309249,28.3342326,1016.02],[118.8308267,28.3342881,1014.15],[118.8307154,28.3342925,1012.34],[118.8306451,28.3342298,1011.35],[118.8305906,28.3341364,1010.4],[118.8305364,28.3340395,1009.37],[118.8305008,28.3339442,1008.77],[118.8304384,28.3338668,1008.34],[118.8303763,28.3337875,1007.97],[118.8302838,28.3338074,1006.46],[118.8301905,28.3338678,1004.09],[118.8301067,28.3339394,1001.64],[118.8300284,28.3340215,1000.15],[118.8299395,28.3340855,999.18],[118.8298851,28.3340334,998.96],[118.8298092,28.3339508,1002.01],[118.8297654,28.3338549,1002.59],[118.8297513,28.3337562,1001.92],[118.8297184,28.333659,1001],[118.8296628,28.3335702,998.95],[118.8296157,28.3334786,996.32],[118.8295582,28.3333976,993.96],[118.8294795,28.3333293,991.99],[118.8293824,28.333286,989.99],[118.8292753,28.3332598,987.72],[118.8291695,28.3332117,985.87],[118.8290863,28.3331503,983.84],[118.8290238,28.3330708,982.53],[118.829004,28.3329745,981.38],[118.82896,28.3328894,977.33],[118.8288874,28.3328249,975.96],[118.8287857,28.3327804,973.69],[118.8286913,28.3327312,971.74],[118.8285912,28.332689,970.46],[118.8284859,28.3326807,968.81],[118.8283709,28.3326772,967.94],[118.8282602,28.3326803,966.33],[118.8281559,28.332702,965.37],[118.8280479,28.3326945,964.33],[118.8279511,28.3326565,962.74],[118.8279338,28.3325697,960.95],[118.8279216,28.3324721,959.53],[118.8279545,28.3323751,958.21],[118.828008,28.3322727,956.4],[118.828008,28.3322727,956.4],[118.828008,28.3322727,956.4],[118.828008,28.3322727,956.4],[118.8280221,28.3321612,954.11],[118.8280221,28.3321612,954.11],[118.8280969,28.3320952,949.69],[118.8281282,28.3320069,947.27],[118.8281021,28.3319162,945.15],[118.8280766,28.3318164,942.35],[118.828021,28.3317259,939.88],[118.8279596,28.3316425,937.65],[118.8279031,28.3315603,935.01],[118.8278374,28.3314851,932.14],[118.8277421,28.3314512,928.79],[118.8276336,28.3314604,926.1],[118.8275622,28.3314127,923.43],[118.8275195,28.331333,920.88],[118.8274965,28.3312402,917.54],[118.8274437,28.3311513,914.25],[118.827393,28.3310628,910.99],[118.8273058,28.3310165,907.65],[118.8272749,28.3309392,905.44],[118.8272395,28.3308553,902.5],[118.8272171,28.3307571,899.63],[118.8272284,28.3306605,897.27],[118.8273033,28.3305999,894.93],[118.8273911,28.3305439,893.08],[118.82747,28.3304793,890.81],[118.8275712,28.330441,888.32],[118.8275418,28.3303898,886.16],[118.8274544,28.3303415,883.09],[118.8273673,28.3302671,880.08],[118.827286,28.3301931,875.25],[118.8272176,28.3301101,871.48],[118.8271577,28.3300288,867.73],[118.827056,28.3299799,863.77],[118.8270218,28.3299021,858.49],[118.8269929,28.3298102,853.57],[118.8269361,28.3297283,849.92],[118.8268747,28.3296516,847.1],[118.8268273,28.3295675,843.77],[118.82675,28.3294989,839.46],[118.8266596,28.3294359,835.36],[118.8265542,28.3293911,831.54],[118.8265771,28.329324,828.08],[118.8266494,28.3292509,823.82],[118.8266494,28.3292509,823.82],[118.8266347,28.329159,821.06],[118.8266347,28.329159,821.06],[118.8265975,28.3290645,818.25],[118.8265265,28.3289942,816.54],[118.8265889,28.3289589,815.08],[118.8266876,28.3289155,812.84],[118.8267027,28.3288318,810.32],[118.8267111,28.3287378,806.66],[118.8267163,28.3286458,802.59],[118.8267293,28.3285479,798.31],[118.8267404,28.3284426,793.34],[118.8267513,28.3283461,789.06],[118.8267375,28.3282547,784.61],[118.8267411,28.3281531,781],[118.8267904,28.3280731,776.83],[118.8268136,28.3279853,770.82],[118.8268736,28.3279121,766.55],[118.8269025,28.3278189,762.7],[118.8269184,28.3277154,758.8],[118.8269359,28.3276135,754.86],[118.8269405,28.3275191,751],[118.8269927,28.3274304,746.76],[118.8269861,28.3273404,736.67],[118.8269693,28.3272491,728.62],[118.8269347,28.3271598,724.18],[118.8270065,28.3270933,716.15],[118.8269583,28.3270264,711.94],[118.8269524,28.3269394,706.82],[118.8270242,28.326874,701.5],[118.8270561,28.3267902,697.92],[118.827024,28.3267024,695.1],[118.8269587,28.3266069,692.26],[118.826898,28.3265074,688.34],[118.8268321,28.3264188,685.34],[118.8267825,28.3263352,682.56],[118.8267195,28.3262642,680.39],[118.8266355,28.3261813,675.49],[118.8265883,28.3260873,670.4],[118.8264835,28.3260853,667.22],[118.8264835,28.3260853,667.22]]";
		bb = 333L;
		double[][] coordinates = parseCoordinates(aa);
		insertCoordinates(coordinates, bb);
	}

	/**
	 * @param coordinates 点位id
	 * @param executeId   执行id
	 * @throws SQLException
	 */
	private void insertCoordinates(double[][] coordinates, Long executeId) throws InterruptedException {
		List<RoutePointEntity> pointList = new ArrayList<>();
		// 遍历经纬度点数组,将每个点的数据插入到数据库表格中

		for (int i = 0; i < coordinates.length; i++) {
			RoutePointEntity routePointEntity = new RoutePointEntity();
			routePointEntity.setLongitude(coordinates[i][0]);
			routePointEntity.setLatitude(coordinates[i][1]);
			routePointEntity.setRouteId(1735589846047338497L);
			routePointEntity.setSort(i+1);
			pointList.add(routePointEntity);
		}
		routePointService.saveBatch(pointList);
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要将GeoJSON边界经纬度存入Java数组,您需要使用一个JSON解析库,比如Jackson或Gson,来解析GeoJSON文件并提取边界坐标。以下是一个使用Jackson库的示例代码: ```java import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; import java.io.File; import java.io.IOException; public class GeoJsonParser { public static void main(String[] args) throws IOException { // 读入GeoJSON文件 File geoJsonFile = new File("path/to/geojson/file.geojson"); ObjectMapper objectMapper = new ObjectMapper(); JsonNode rootNode = objectMapper.readTree(geoJsonFile); // 提取边界坐标 ArrayNode coordinates = (ArrayNode) rootNode .path("features") .get(0) .path("geometry") .path("coordinates"); // 将坐标存入数组 double[][] boundary = new double[coordinates.size()][2]; for (int i = 0; i < coordinates.size(); i++) { boundary[i][0] = coordinates.get(i).get(0).asDouble(); boundary[i][1] = coordinates.get(i).get(1).asDouble(); } // 打印数组 for (int i = 0; i < boundary.length; i++) { System.out.println("[" + boundary[i][0] + "," + boundary[i][1] + "]"); } } } ``` 在这个示例中,我们首先使用Jackson库读入GeoJSON文件。然后,我们使用JsonNode对象的path()方法来提取边界坐标。边界坐标存储在一个二维数组中,我们使用for循环将每个坐标存入数组中。最后,我们打印数组中的每个坐标。注意,这个示例假设GeoJSON文件中只有一个Feature,如果有多个Feature,需要在循环中处理每个Feature的边界坐标。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值