wkt转geojson
RT
/** * json格式转wkt格式 * * @param code * @param wkt * @return * @throws SchemaException */ private void getGeometryJSON(String rootCode) throws SchemaException { // geometry是必须的,其他属性可根据需求自定义,但是支持的类型有限,例如这个版本中double是不支持的,只支持float final SimpleFeatureType TYPE = DataUtilities.createType("Polygon", "geometry:MultiPolygon, id:String, name:String"); SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE); String ret = null; // 设置保留6位小数,否则GeometryJSON默认保留4位小数 GeometryJSON gjson = new GeometryJSON(6); FeatureJSON fjson = new FeatureJSON(gjson); List<SimpleFeature> features = new ArrayList<>(); SimpleFeatureCollection collection = new ListFeatureCollection(TYPE, features); Geometry geometry = null; try (StringWriter writer = new StringWriter();) { // 循环获取需要组装的数据 for (String cd : codes) { String wkt = getWktData(cd); geometry = getGeometry(wkt); if (geometry == null) { continue; } featureBuilder.add(geometry); featureBuilder.add(cd); featureBuilder.add("name-" + cd); SimpleFeature feature = featureBuilder.buildFeature(null); features.add(feature); } fjson.writeFeatureCollection(collection, writer); ret = writer.toString(); } catch (Exception e) { LOGGER.error("失败:" + e.getMessage()); } if (StringUtil.isEmpty(ret)) { LOGGER.error("数据为空"); throw new CommonException(CommonErrorCodeEnum.ERROR_BUSINESS); } String geometryJSONStr = EchartMapDataConvert.convert2EchartsByStr(ret, rootCode, "json"); // 将GeometryJSON数据保存到文件 FileUtil.writeStringToFile(geometryJSONStr, "D:\\wind\\mapjson\\" + rootCode + ".json"); } /** * 从数据库获取AOI要素,并将多个POLYGON组装成MultiPolygon * * @param code * @return */ private String getWktData(String code) { Long bgnTime = System.currentTimeMillis(); List<CountryAreaInfo> areas = countryAreaInfoService .selectByAreaCode(new String[] { code }); // .selectByAreaCode(new String[] { "156000000" }); Long endTime = System.currentTimeMillis(); LOGGER.info("tb_country_area_info 数据查询用时:" + (endTime - bgnTime)); if (CollectionUtils.isEmpty(areas)) { return null; } if (areas.size() == 1) { // 单面数据直接返回 return areas.get(0).getOgcGeom(); } StringBuilder sb = new StringBuilder(start); for (CountryAreaInfo area : areas) { sb.append(area.getOgcGeom().replaceFirst("POLYGON", "")) .append(","); } sb.replace(sb.length() - 1, sb.length(), end); Long endTime2 = System.currentTimeMillis(); LOGGER.info("数据组装用时:" + (endTime2 - endTime)); return sb.toString(); } /** * 读取wkt流 * * @param wkt * @return * @throws ParseException */ private Geometry getGeometry(String wkt) throws ParseException { if (StringUtil.isEmpty(wkt)) { return null; } WKTReader reader = new WKTReader(geometryFactory); return reader.read(wkt); }

浙公网安备 33010602011771号