From 4412d512c32fe4515657ee90e90bfcc27962add0 Mon Sep 17 00:00:00 2001 From: lang Date: Wed, 13 Jan 2016 16:29:21 +0800 Subject: [PATCH] Remove map tool --- map/tool/compress.js | 82 ---------------- map/tool/fetchAMap.js | 60 ------------ map/tool/genGeo.js | 215 ------------------------------------------ map/tool/jsTpl.js | 27 ------ map/tool/simplify.js | 121 ------------------------ 5 files changed, 505 deletions(-) delete mode 100644 map/tool/compress.js delete mode 100644 map/tool/fetchAMap.js delete mode 100644 map/tool/genGeo.js delete mode 100644 map/tool/jsTpl.js delete mode 100644 map/tool/simplify.js diff --git a/map/tool/compress.js b/map/tool/compress.js deleted file mode 100644 index 0c8249c31..000000000 --- a/map/tool/compress.js +++ /dev/null @@ -1,82 +0,0 @@ - -function compress(json) { - - json.UTF8Encoding = true; - - var features = json.features; - - features.forEach(function (feature){ - var encodeOffsets = feature.geometry.encodeOffsets = []; - var coordinates = feature.geometry.coordinates; - 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] = [] - ); - }); - }); - } - else { - coordinates.forEach(function (coordinate, idx){ - coordinates[idx] = encodePolygon( - coordinate, encodeOffsets[idx] = [] - ); - }); - } - }); - - return json; -} - -function encodePolygon(coordinates, encodeOffsets) { - - var result = ''; - - var prevX = quantize(coordinates[0][0]); - var prevY = quantize(coordinates[0][1]); - // Store the origin offset - encodeOffsets[0] = prevX; - encodeOffsets[1] = prevY; - - for (var i = 0; i < coordinates.length; i++) { - var point = coordinates[i]; - result += encode(point[0], prevX); - result += encode(point[1], prevY); - - prevX = quantize(point[0]); - prevY = quantize(point[1]); - } - - return result; -} - -function quantize(val) { - return Math.ceil(val * 1024); -} - -function encode(val, prev){ - // Quantization - val = quantize(val); - // var tmp = val; - // Delta - var delta = val - prev; - - if (((delta << 1) ^ (delta >> 15)) + 64 === 8232) { - //WTF, 8232 will get syntax error in js code - delta--; - } - // ZigZag - delta = (delta << 1) ^ (delta >> 15); - // add offset and get unicode - return String.fromCharCode(delta + 64); - // var tmp = {'tmp' : str}; - // try{ - // eval("(" + JSON.stringify(tmp) + ")"); - // }catch(e) { - // console.log(val + 64); - // } -} - -module.exports = compress; \ No newline at end of file diff --git a/map/tool/fetchAMap.js b/map/tool/fetchAMap.js deleted file mode 100644 index bcefa980c..000000000 --- a/map/tool/fetchAMap.js +++ /dev/null @@ -1,60 +0,0 @@ -var request = require('request'); -var fs = require('fs'); - -var provinceList = ['台湾省', '河北省', '山西省', '内蒙古自治区', '辽宁省', '吉林省','黑龙江省', '江苏省', '浙江省', '安徽省', '福建省', '江西省', '山东省','河南省', '湖北省', '湖南省', '广东省', '广西壮族自治区', '海南省', '四川省', '贵州省', '云南省', '西藏自治区', '陕西省', '甘肃省', '青海省', '宁夏回族自治区', '新疆维吾尔自治区']; -// var provinceList = ['台湾', '河北', '山西', '内蒙古', '辽宁', '吉林','黑龙江', '江苏', '浙江', '安徽', '福建', '江西', '山东','河南', '湖北', '湖南', '广东', '广西', '海南', '四川', '贵州', '云南', '西藏', '陕西', '甘肃', '青海', '宁夏', '新疆']; - -var centralCityFullList = ['北京市', '天津市', '上海市', '重庆市', '香港特别行政区', '澳门特别行政区' -]; - -var API_BASE = 'http://restapi.amap.com/v3/config/'; -var API_KEY = 'd619ce5126cfe80ad9c645fcc1b54e52'; - -function makeRequestUrl (obj, type) { - var paramStr = ''; - for (var name in obj) { - paramStr += (paramStr ? '&' : '?') + name + '=' + encodeURIComponent(obj[name]); - } - paramStr += '&key=' + API_KEY + '&output=json'; - // console.log(API_BASE + type + paramStr); - return API_BASE + type + paramStr; -} - -function makeDistritSearchUrl(obj) { - return makeRequestUrl({ - extensions: 'all', - keywords: obj.keywords, - subdistrict: 1 - }, 'district'); -} - -provinceList.forEach(function (provinceName) { - request(makeDistritSearchUrl({ - keywords: provinceName, - level: 'province' - }), function (err, response, body) { - if (!fs.existsSync('./tmp/' + provinceName)) { - fs.mkdirSync('./tmp/' + provinceName); - } - fs.writeFileSync('./tmp/' + provinceName +'.json', body, 'utf-8'); - - var res = JSON.parse(body); - res.districts[0].districts.forEach(function (city) { - request(makeDistritSearchUrl({ - keywords: city.name, - level: 'city' - }), function (err, response, body) { - fs.writeFileSync('./tmp/' + provinceName + '/' + city.name +'.json', body, 'utf-8'); - }); - }); - }); -}); - -centralCityFullList.forEach(function (cityName) { - request(makeDistritSearchUrl({ - keywords: cityName, - level: 'city' - }), function (err, response, body) { - fs.writeFileSync('./tmp/' + cityName +'.json', body, 'utf-8'); - }); -}); \ No newline at end of file diff --git a/map/tool/genGeo.js b/map/tool/genGeo.js deleted file mode 100644 index c22fff6c9..000000000 --- a/map/tool/genGeo.js +++ /dev/null @@ -1,215 +0,0 @@ -var fs = require('fs'); -var compress = require('./compress'); -var simplify = require('./simplify'); -var pinyin = require('pinyin'); - -var jsTplStr = fs.readFileSync('./jsTpl.js', 'utf-8'); - -var provinceList = ['台湾', '河北', '山西', '内蒙古', '辽宁', '吉林','黑龙江', '江苏', '浙江', '安徽', '福建', '江西', '山东','河南', '湖北', '湖南', '广东', '广西', '海南', '四川', '贵州', '云南', '西藏', '陕西', '甘肃', '青海', '宁夏', '新疆']; - -var provinceFullList = ['台湾省', '河北省', '山西省', '内蒙古自治区', '辽宁省', '吉林省','黑龙江省', '江苏省', '浙江省', '安徽省', '福建省', '江西省', '山东省','河南省', '湖北省', '湖南省', '广东省', '广西壮族自治区', '海南省', '四川省', '贵州省', '云南省', '西藏自治区', '陕西省', '甘肃省', '青海省', '宁夏回族自治区', '新疆维吾尔自治区']; - -var centralCityFullList = ['北京市', '天津市', '上海市', '重庆市', '香港特别行政区', '澳门特别行政区' -]; - -var centralCityList = ['北京', '天津', '上海', '重庆', '香港', '澳门']; - - -// var provinceShortFullMap = provinceList.reduce(function (obj, val, idx) { -// return (obj[val] = provinceFullList[idx]); -// }, {}); -var provinceFullShortMap = provinceFullList.reduce(function (obj, val, idx) { - obj[val] = provinceList[idx]; - return obj; -}, {}); -var centralCityFullShortMap = centralCityFullList.reduce(function (obj, val, idx) { - obj[val] = centralCityList[idx]; - return obj; -}, {}); - -function polygonArea(pts) { - var area = 0; - for (var i = pts.length - 1, j = 0; j < pts.length;) { - var pt0 = pts[0]; - var pt1 = pts[1]; - i = j; - j++; - area += pt0[0] * pt1[1] - pt1[0] * pt0[1]; - } - return Math.abs(area); -} - -function makeFeature(district, minArea, simplifyRatio) { - var boundaries = district.polyline.split('|').map(function (str) { - return str.split(';').map(function (strPt) { - var pt = strPt.split(','); - return [+pt[0], +pt[1]]; - }); - }).map(function (polygon) { - return simplify(polygon, simplifyRatio, true); - }).filter(function (polygon) { - // Remove small area isload - return polygonArea(polygon) > minArea; - }); - if (district.name === '海南省') { - var maxArea = 0; - var maxAreaPolygon; - for (var i = 0; i < boundaries.length; i++) { - var area = polygonArea(boundaries[i]); - if (area > maxArea) { - maxArea = area; - maxAreaPolygon = boundaries[i]; - } - } - // 去除南海诸岛 - boundaries = [maxAreaPolygon]; - } - var feature = { - id: district.adcode, - geometry: { - type: boundaries.length > 1 ? 'MultiPolygon' : 'Polygon', - coordinates: boundaries.length > 1 ? [boundaries] : boundaries - }, - properties: { - cp: district.center.split(',').map(function (a) {return +a;}), - name: centralCityFullShortMap[district.name] - || provinceFullShortMap[district.name] || district.name, - childNum: boundaries.length - } - }; - - return feature; -} - - -function readProvince(provinceName) { - return JSON.parse(fs.readFileSync('./tmp/' + provinceName + '.json', 'utf-8')); -} - -function readCity(provinceName, cityName) { - return JSON.parse(fs.readFileSync('./tmp/' + provinceName + '/' + cityName + '.json', 'utf-8')); -} - -function makeChina() { - var geoJson = { - type: 'FeatureCollection', - features: provinceFullList.map(function (provinceName) { - var json = readProvince(provinceName); - return makeFeature(json.districts[0], 7, 0.015); - }) - }; - geoJson.features = geoJson.features.concat(centralCityFullList.map(function (cityName) { - var json = readProvince(cityName); - return makeFeature(json.districts[0], 7, 0.015); - })); - var jsonStr = JSON.stringify(compress(geoJson)); - fs.writeFileSync( - '../json/china.json', - jsonStr, - 'utf-8' - ); - fs.writeFileSync( - '../js/china.js', - jsTplStr.replace('{{name}}', 'china') - .replace('{{data}}', jsonStr), - 'utf-8' - ); -} - -function makeProvince(provinceName) { - - var provinceDetail = readProvince(provinceName); - - var geoJson = { - type: 'FeatureCollection', - features: provinceDetail.districts[0].districts.map(function (city) { - var cityCode = city.citycode; - var cityDetail = readCity(provinceName, city.name); - return makeFeature(cityDetail.districts.filter(function (distrct) { - return distrct.citycode === cityCode; - })[0], 0, 0.0005); - }) - }; - - var jsonStr = JSON.stringify(compress(geoJson)); - var pinyinName = pinyin(provinceFullShortMap[provinceName], { - // heteronym: true, - style: pinyin.STYLE_NORMAL - }).join(''); - fs.writeFileSync( - '../json/province/' + pinyinName + '.json', - jsonStr, - 'utf-8' - ); - - fs.writeFileSync( - '../js/province/' + pinyinName + '.js', - jsTplStr.replace('{{name}}', provinceFullShortMap[provinceName]) - .replace('{{data}}', jsonStr), - 'utf-8' - ); -} - -makeChina(); - -provinceFullList.forEach(function (provinceName) { - if (provinceName === '台湾省') { - return; - } - console.log('Generating ' + provinceName); - makeProvince(provinceName); -}); - -var worldNameMap = { - // 'United States': 'United States of America', - // 'Côte d\'Ivoire': 'Ivory Coast', - // 'Central African Rep.': 'Central African Republic', - // 'S. Sudan': 'South Sudan', - // 'Dem. Rep. Congo': 'Democratic Republic of the Congo', - 'Republic of Congo': 'Republic of the Congo' -}; -// World -var worldGeo = JSON.parse(fs.readFileSync('./world/tmp/world.json', 'utf-8')); -var taiwanFeature; -var chinaFeature; -worldGeo.features = worldGeo.features.filter(function (feature) { - if (feature.properties && feature.properties.name === 'Taiwan') { - taiwanFeature = feature; - return false; - } - if (feature.properties && feature.properties.name === 'China') { - chinaFeature = feature; - } - return feature.geometry && feature.properties - && feature.properties.name !== 'Antarctica'; -}).map(function (feature) { - var res = { - geometry: { - type: feature.geometry.type, - coordinates: feature.geometry.coordinates - }, - properties: { - name: worldNameMap[feature.properties.sovereignt] - || feature.properties.sovereignt, - childNum: feature.geometry.coordinates.length - } - }; - if (feature.properties.cp) { - res.properties.cp = feature.properties.cp; - } - return res; -}); - -if (taiwanFeature) { - chinaFeature.geometry.coordinates.push(taiwanFeature.geometry.coordinates); - chinaFeature.properties.childNum++; -} - -var worldGeoStr = JSON.stringify(worldGeo); -fs.writeFileSync('../json/world.json', worldGeoStr , 'utf-8'); -fs.writeFileSync( - '../js/world.js', - jsTplStr.replace('{{name}}', 'world') - .replace('{{data}}', worldGeoStr), - 'utf-8' -); \ No newline at end of file diff --git a/map/tool/jsTpl.js b/map/tool/jsTpl.js deleted file mode 100644 index 58a77bdb9..000000000 --- a/map/tool/jsTpl.js +++ /dev/null @@ -1,27 +0,0 @@ -(function (root, factory) { - if (typeof define === 'function' && define.amd) { - // AMD. Register as an anonymous module. - define(['exports', 'echarts'], factory); - } else if (typeof exports === 'object' && typeof exports.nodeName !== 'string') { - // CommonJS - factory(exports, require('echarts')); - } else { - // Browser globals - 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('{{name}}', {{data}}); -})); \ No newline at end of file diff --git a/map/tool/simplify.js b/map/tool/simplify.js deleted file mode 100644 index 515218d2b..000000000 --- a/map/tool/simplify.js +++ /dev/null @@ -1,121 +0,0 @@ -/* - (c) 2013, Vladimir Agafonkin - Simplify.js, a high-performance JS polyline simplification library - mourner.github.io/simplify-js -*/ - -(function () { 'use strict'; - -// to suit your point format, run search/replace for '[0]' and '[1]'; -// for 3D version, see 3d branch (configurability would draw significant performance overhead) - -// square distance between 2 points -function getSqDist(p1, p2) { - - var dx = p1[0] - p2[0], - dy = p1[1] - p2[1]; - - return dx * dx + dy * dy; -} - -// square distance from a point to a segment -function getSqSegDist(p, p1, p2) { - - var x = p1[0], - y = p1[1], - dx = p2[0] - x, - dy = p2[1] - y; - - if (dx !== 0 || dy !== 0) { - - var t = ((p[0] - x) * dx + (p[1] - y) * dy) / (dx * dx + dy * dy); - - if (t > 1) { - x = p2[0]; - y = p2[1]; - - } else if (t > 0) { - x += dx * t; - y += dy * t; - } - } - - dx = p[0] - x; - dy = p[1] - y; - - return dx * dx + dy * dy; -} -// rest of the code doesn't care about point format - -// basic distance-based simplification -function simplifyRadialDist(points, sqTolerance) { - - var prevPoint = points[0], - newPoints = [prevPoint], - point; - - for (var i = 1, len = points.length; i < len; i++) { - point = points[i]; - - if (getSqDist(point, prevPoint) > sqTolerance) { - newPoints.push(point); - prevPoint = point; - } - } - - if (prevPoint !== point) newPoints.push(point); - - return newPoints; -} - -function simplifyDPStep(points, first, last, sqTolerance, simplified) { - var maxSqDist = sqTolerance, - index; - - for (var i = first + 1; i < last; i++) { - var sqDist = getSqSegDist(points[i], points[first], points[last]); - - if (sqDist > maxSqDist) { - index = i; - maxSqDist = sqDist; - } - } - - if (maxSqDist > sqTolerance) { - if (index - first > 1) simplifyDPStep(points, first, index, sqTolerance, simplified); - simplified.push(points[index]); - if (last - index > 1) simplifyDPStep(points, index, last, sqTolerance, simplified); - } -} - -// simplification using Ramer-Douglas-Peucker algorithm -function simplifyDouglasPeucker(points, sqTolerance) { - var last = points.length - 1; - - var simplified = [points[0]]; - simplifyDPStep(points, 0, last, sqTolerance, simplified); - simplified.push(points[last]); - - return simplified; -} - -// both algorithms combined for awesome performance -function simplify(points, tolerance, highestQuality) { - - if (points.length <= 2) return points; - - var sqTolerance = tolerance !== undefined ? tolerance * tolerance : 1; - - points = highestQuality ? points : simplifyRadialDist(points, sqTolerance); - points = simplifyDouglasPeucker(points, sqTolerance); - - return points; -} - -// export as AMD module / Node module / browser or worker variable -if (typeof define === 'function' && define.amd) define(function() { return simplify; }); -else if (typeof module !== 'undefined') module.exports = simplify; -else if (typeof self !== 'undefined') self.simplify = simplify; -else window.simplify = simplify; - -})(); \ No newline at end of file -- GitLab