Skip to content

Commit 330b87b

Browse files
修复 ol webmap csv 处理报错
1 parent b28d0c4 commit 330b87b

File tree

2 files changed

+224
-3
lines changed

2 files changed

+224
-3
lines changed

src/openlayers/mapping/WebMap.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2641,10 +2641,13 @@ export class WebMap extends Observable {
26412641
if (feature) {
26422642
let newFeature = (window.cloneDeep || cloneDeep)(feature);
26432643
newFeature.properties = {};
2644+
const titleLen = titles.length;
26442645
row.forEach((item, idx) => {
26452646
//空格问题,看见DV多处处理空格问题,TODO统一整理
2646-
let key = titles[idx].trim();
2647-
newFeature.properties[key] = item;
2647+
if (idx < titleLen) {
2648+
let key = titles[idx].trim();
2649+
newFeature.properties[key] = item;
2650+
}
26482651
});
26492652
geojson.features.push(newFeature);
26502653
}

test/openlayers/mapping/WebMapSpec.js

Lines changed: 219 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import Feature from 'ol/Feature';
2424
import * as olProj from 'ol/proj';
2525
import proj4 from 'proj4';
2626
import * as olLayer from 'ol/layer';
27+
import cloneDeep from 'lodash.clonedeep';
2728

2829
window.jsonsql = { query: () => { } };
2930

@@ -1939,5 +1940,222 @@ describe('openlayers_WebMap', () => {
19391940
done();
19401941
}
19411942
});
1942-
1943+
1944+
it('fix csv property length over title length', (done) => {
1945+
window.cloneDeep = function(feature) {
1946+
return cloneDeep(feature);
1947+
}
1948+
let options = {
1949+
server: server,
1950+
webMap: defaultServeRequest,
1951+
successCallback,
1952+
errorCallback: function () { }
1953+
};
1954+
spyOn(FetchRequest, 'get').and.callFake((url) => {
1955+
if (url.indexOf('map.json') > -1) {
1956+
var mapJson = datavizWebMap_noServerIdMarker;
1957+
return Promise.resolve(new Response(mapJson));
1958+
}
1959+
return Promise.resolve();
1960+
});
1961+
var datavizWebmap = new WebMap(options);
1962+
function successCallback() {
1963+
const features = [
1964+
{
1965+
"geometry": {
1966+
"coordinates": [
1967+
[
1968+
[
1969+
117.9612792600001,
1970+
31.50179038417261
1971+
],
1972+
[
1973+
117.6538350116667,
1974+
31.36421118000805
1975+
],
1976+
[
1977+
117.5036929400001,
1978+
31.00564246918011
1979+
],
1980+
[
1981+
117.2036532125,
1982+
31.06033994834594
1983+
],
1984+
[
1985+
117.0296223400001,
1986+
31.2610837141762
1987+
],
1988+
[
1989+
117.1815134989584,
1990+
31.55480553729684
1991+
],
1992+
[
1993+
116.7638726916668,
1994+
31.61569306000427
1995+
],
1996+
[
1997+
116.7663789980209,
1998+
31.90478239979175
1999+
],
2000+
[
2001+
117.0149495701043,
2002+
32.09548291728905
2003+
],
2004+
[
2005+
116.9956159214584,
2006+
32.11601320312207
2007+
],
2008+
[
2009+
117.0107175175001,
2010+
32.16395522082976
2011+
],
2012+
[
2013+
117.00395803,
2014+
32.39582160415978
2015+
],
2016+
[
2017+
117.1965708000001,
2018+
32.53416217415796
2019+
],
2020+
[
2021+
117.3470498758334,
2022+
32.45553657999236
2023+
],
2024+
[
2025+
117.3796553844792,
2026+
32.43855426426338
2027+
],
2028+
[
2029+
117.4034777350001,
2030+
32.38868854999331
2031+
],
2032+
[
2033+
117.3973120916668,
2034+
32.29735015499454
2035+
],
2036+
[
2037+
117.5562772865626,
2038+
32.22039671520397
2039+
],
2040+
[
2041+
117.8666145383334,
2042+
32.13696457999682
2043+
],
2044+
[
2045+
117.8979449009376,
2046+
31.93163098958303
2047+
],
2048+
[
2049+
117.9540050100001,
2050+
31.90536486416671
2051+
],
2052+
[
2053+
117.9612792600001,
2054+
31.50179038417261
2055+
]
2056+
]
2057+
],
2058+
"type": "Polygon"
2059+
},
2060+
"properties": {
2061+
"Name": "合肥市",
2062+
"PAC": 340102,
2063+
"UserID": 0,
2064+
"Province": "安徽省"
2065+
},
2066+
"type": "Feature"
2067+
},{
2068+
"geometry": {
2069+
"coordinates": [
2070+
[
2071+
[
2072+
116.9620359318751,
2073+
30.63692908460247
2074+
],
2075+
[
2076+
116.9626803625001,
2077+
30.63792411335244
2078+
],
2079+
[
2080+
116.9636623712501,
2081+
30.63948495199823
2082+
],
2083+
[
2084+
116.9647166275001,
2085+
30.64103160251904
2086+
],
2087+
[
2088+
116.9655092789584,
2089+
30.64199830803986
2090+
],
2091+
[
2092+
116.9661609391667,
2093+
30.64248322168568
2094+
],
2095+
[
2096+
116.9668600346876,
2097+
30.64271652616482
2098+
],
2099+
[
2100+
116.9676117783334,
2101+
30.64279261251899
2102+
],
2103+
[
2104+
116.9683829830209,
2105+
30.64278320543566
2106+
],
2107+
[
2108+
116.9691700758334,
2109+
30.64280515585233
2110+
]
2111+
]
2112+
],
2113+
"type": "Polygon"
2114+
},
2115+
"properties": {
2116+
"Name": "安庆市",
2117+
"PAC": 340823,
2118+
"UserID": 0,
2119+
"Province": "安徽省"
2120+
},
2121+
"type": "Feature"
2122+
}];
2123+
const datas = [[
2124+
"SmUserID",
2125+
"NAME",
2126+
"CODE",
2127+
"PROVINCE",
2128+
"Geometry"
2129+
],[
2130+
"0",
2131+
"安庆市",
2132+
"130100",
2133+
"HEBEI",
2134+
"POLYGON ((113.8395751199999495 38.7583869546093496",
2135+
"113.8408729199999811 38.7573249546098353",
2136+
"113.8452159599999476 38.7550289646109007",
2137+
"113.8183570799999416 38.7584829846093086",
2138+
"113.8292251199999754 38.7564389946102565",
2139+
"113.8395751199999495 38.7583869546093496))"
2140+
],
2141+
[
2142+
"1",
2143+
"合肥市",
2144+
"130100",
2145+
"HEBEI",
2146+
"POLYGON ((113.8395751199999495 38.7583869546093496",
2147+
"113.8408729199999811 38.7573249546098353",
2148+
"113.8452159599999476 38.7550289646109007",
2149+
"113.8183570799999416 38.7584829846093086",
2150+
"113.8292251199999754 38.7564389946102565",
2151+
"113.8395751199999495 38.7583869546093496))"
2152+
]];
2153+
const divisionType = 'City';
2154+
const divisionField = 'NAME';
2155+
const geojson = datavizWebmap.changeExcel2Geojson(features, datas, divisionType, divisionField);
2156+
expect(geojson.features.length).toBe(2);
2157+
expect(geojson.features[0].properties.hasOwnProperty(undefined)).toBeFalse();
2158+
done();
2159+
}
2160+
});
19432161
});

0 commit comments

Comments
 (0)