更改函数以接受多个参数

时间:2017-11-15 10:14:26

标签: javascript function

我需要修改下面的函数来接受多个参数而不是只有4个参数,我已经能够成功修改第一个函数UseWicketToGoFromGooglePolysToWKT但是我无法修改UseJstsToDissolveGeometries因为我我不确定如何遍历参数并分配一个变量名称以便稍后在函数中使用,我也得到错误Uncaught TypeError: Cannot read property 'union' of undefined

如何修改UseJstsToDissolveGeometries以接受多个参数?

function UseWicketToGoFromGooglePolysToWKT(poly1,poly2,poly3,poly4) {
   var wicket = new Wkt.Wkt();

   wicket.fromObject(poly1);
   var wkt1 = wicket.write();

   wicket.fromObject(poly2);
   var wkt2 = wicket.write();

   wicket.fromObject(poly3);
   var wkt3 = wicket.write();

   wicket.fromObject(poly4);
   var wkt4 = wicket.write();

   return [wkt1, wkt2, wkt3, wkt4];
}

function UseJstsToDissolveGeometries(wkt1, wkt2, wkt3, wkt4) {
  // Instantiate JSTS WKTReader and get two JSTS geometry objects
  var wktReader = new jsts.io.WKTReader();
  var geom1 = wktReader.read(wkt1);
  var geom2 = wktReader.read(wkt2);
  var geom3 = wktReader.read(wkt3);
  var geom4 = wktReader.read(wkt4);

  // In JSTS, "union" is synonymous with "dissolve"

  var dissolvedGeometry;
  dissolvedGeometry = geom2.union(geom1);
  dissolvedGeometry = dissolvedGeometry.union(geom3);
  dissolvedGeometry = dissolvedGeometry.union(geom4);

  // Instantiate JSTS WKTWriter and get new geometry's WKT

  var wktWriter = new jsts.io.WKTWriter();
  var wkt = wktWriter.write(dissolvedGeometry);

  // Use Wicket to ingest the new geometry's WKT

  var wicket = new Wkt.Wkt();
  wicket.read(wkt);
}

function DoEverything() {
   var wkt = UseWicketToGoFromGooglePolysToWKT(polygon6, polygon7,polygon8, polygon9);
   UseJstsToDissolveGeometries(wkt[0], wkt[1], wkt[2], wkt[3]);
}

我的尝试

function UseWicketToGoFromGooglePolysToWKT(...args) {
   var wicket = new Wkt.Wkt();
   return args.map(item => {
     wicket.fromObject(item);
     return wicket.write();
   })
}

function UseJstsToDissolveGeometries(...args) {
  var wktReader = new jsts.io.WKTReader();
  args.map(item => {
    return wktReader.read(item);
  })

  var dissolvedGeometry;
  args.map(item => {
    return dissolvedGeometry = dissolvedGeometry.union(item)
  })

  var wktWriter = new jsts.io.WKTWriter();
  var wkt = wktWriter.write(dissolvedGeometry);

  // Use Wicket to ingest the new geometry's WKT
  var wicket = new Wkt.Wkt();
  wicket.read(wkt);
}

function DoEverything() {
   var wkt = UseWicketToGoFromGooglePolysToWKT(polygon6, polygon7,polygon8, polygon9);
   UseJstsToDissolveGeometries(wkt[0], wkt[1], wkt[2], wkt[3]);
}

1 个答案:

答案 0 :(得分:1)

我相信你正在寻找这样的东西,问题在于你正在使用 dissolvedGeometry在定义之前

function UseJstsToDissolveGeometries(...args) {
  var wktReader = new jsts.io.WKTReader();
  var geoms = args.map(item => {
    return wktReader.read(item);
  })

  var dissolvedGeometry = geoms.reduce((accumulator , item, currentIndex, array) => {
    if (currentIndex == 0) {
      return array[1].union(item);
    }

    if(currentIndex == 1){
      return accumulator;
    }

    return accumulator.union(item)
  })

  var wktWriter = new jsts.io.WKTWriter();
  var wkt = wktWriter.write(dissolvedGeometry);

  // Use Wicket to ingest the new geometry's WKT
  var wicket = new Wkt.Wkt();
  wicket.read(wkt);
}

此外,您需要.map的结果。另外,因为您只需要一个结果使用dissolvedGeometry,我认为.reduce会更好

PS 因为我实际上并不知道这些对象产生了什么,它们有什么方法等等,这些代码可能不会工作但会让你知道什么做到这一点