echart地图数据压缩
引入js工具方法
function convert2EchartsByStr(rawStr,fileName,type){ var json = JSON.parse(rawStr); return convert2Echarts(json,fileName,type); } function convert2Echarts(json,fileName,type){ // Meta tag var results=""; json.UTF8Encoding = true; var features = json.features; if (!features) { return; } features.forEach(function (feature){ var encodeOffsets = feature.geometry.encodeOffsets = []; var coordinates = feature.geometry.coordinates; if (feature.geometry.type === 'Polygon') { coordinates.forEach(function (coordinate, idx){ coordinates[idx] = encodePolygon( coordinate, encodeOffsets[idx] = [] ); }); } else if(feature.geometry.type === 'MultiPolygon') { coordinates.forEach(function (polygon, idx1){ encodeOffsets[idx1] = []; polygon.forEach(function (coordinate, idx2) { coordinates[idx1][idx2] = encodePolygon( coordinate, encodeOffsets[idx1][idx2] = [] ); }); }); } }); if(type==='json'){ results=JSON.stringify(json); }else{ results=addEchartsJsWrapper(JSON.stringify(json), fileName); } return results; }; function encodePolygon(coordinate, encodeOffsets) { var result = ''; var prevX = quantize(coordinate[0][0]); var prevY = quantize(coordinate[0][1]); // Store the origin offset encodeOffsets[0] = prevX; encodeOffsets[1] = prevY; for (var i = 0; i < coordinate.length; i++) { var point = coordinate[i]; result+=encode(point[0], prevX); result+=encode(point[1], prevY); prevX = quantize(point[0]); prevY = quantize(point[1]); } return result; } function addAMDWrapper(jsonStr) { return ['define(function() {', ' return ' + jsonStr + ';', '});'].join('\n'); } function addEchartsJsWrapper(jsonStr,fileName) { return ['(function (root, factory) {', " if (typeof define === 'function' && define.amd) {", " define(['exports', 'echarts'], factory);", " } else if (typeof exports === 'object' && typeof exports.nodeName !== 'string') {", " factory(exports, require('echarts'));", " } else {", " factory({}, root.echarts);", " }", " }(this, function (exports, echarts) {", " var log = function (msg) {", " if (typeof console !== 'undefined') {", " console && console.error && console.error(msg);", " }", " }", " if (!echarts) {", " log('ECharts is not Loaded');", " return;", " }", " if (!echarts.registerMap) {", " log('ECharts Map is not loaded')", " return;", " }", " echarts.registerMap('"+fileName+"',"+ jsonStr, ' )}));'].join('\n'); } function encode(val, prev){ // Quantization val = quantize(val); // var tmp = val; // Delta val = val - prev; if (((val << 1) ^ (val >> 15)) + 64 === 8232) { //WTF, 8232 will get syntax error in js code val--; } // ZigZag val = (val << 1) ^ (val >> 15); // add offset and get unicode return String.fromCharCode(val+64); // var tmp = {'tmp' : str}; // try{ // eval("(" + JSON.stringify(tmp) + ")"); // }catch(e) { // console.log(val + 64); // } } function quantize(val) { return Math.ceil(val * 1024); }
定义工具类
import java.io.FileNotFoundException; import java.io.FileReader; import javax.script.Invocable; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; /** * echart地图数据格式压缩解压逻辑 * */ public class EchartMapDataConvert { //1.得到脚本引擎 public static final ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); static { try { //2.引擎读取脚本字符串 engine.eval(new FileReader(EchartMapDataConvert.class.getResource("/echart_map_date_convert.js").getPath())); } catch (ScriptException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } } // 3.将引擎转换为Invocable,这样才可以掉用js的方法 public static final Invocable invocable = (Invocable) engine; /** * 压缩为 echarts 格式的地图 * * @param geoJson 未转义的json字符串 * @param fileName * @param type json 或 其他(按js处理) * @return */ public static String convert2Echarts(String geoJson, String fileName, String type) { try { //4.使用 invocable.invokeFunction掉用js脚本里的方法,第一個参数为方法名,后面的参数为被调用的js方法的入参 return (String) invocable.invokeFunction("convert2Echarts", geoJson, fileName, type); } catch (ScriptException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } } /** * 压缩为 echarts 格式的地图 * * @param geoJson 转义后的json字符串 * @param fileName * @param type json 或 其他(按js处理) * @return */ public static String convert2EchartsByStr(String geoJson, String fileName, String type) { try { //4.使用 invocable.invokeFunction掉用js脚本里的方法,第一個参数为方法名,后面的参数为被调用的js方法的入参 return (String) invocable.invokeFunction("convert2EchartsByStr", geoJson, fileName, type); } catch (ScriptException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } } }
调用
public static void main(String[] args) { String ret = FileUtil.readFileToString("D:\\wind\\mapjson\\051000000.json", "UTF-8"); String geometryJSONStr = EchartMapDataConvert.convert2EchartsByStr(ret, "051000000", "json"); // 将GeometryJSON数据保存到文件 FileUtil.writeStringToFile(geometryJSONStr, "D:\\wind\\mapjson\\aaaa.json"); }

浙公网安备 33010602011771号