Changeset 31044 in osm for applications/editors/josm/plugins/seachart/src/s57/S57map.java
- Timestamp:
- 2015-03-03T17:08:26+01:00 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/seachart/src/s57/S57map.java
r31031 r31044 191 191 public int outers; // Number of outers 192 192 public int inners; // Number of inners 193 public ArrayList<Comp> refs; // Ordered list of compounds193 public ArrayList<Comp> comps; // Ordered list of compounds 194 194 public double area; // Area of feature 195 195 public double length; // Length of feature … … 199 199 elems = new ArrayList<Prim>(); 200 200 outers = inners = 0; 201 refs = new ArrayList<Comp>();201 comps = new ArrayList<Comp>(); 202 202 area = 0; 203 203 length = 0; … … 245 245 private Feature feature; 246 246 private Edge edge; 247 private KeyVal<?> osm = S57osm.OSMtag("", ""); 247 248 248 249 public S57map() { … … 388 389 feature.geom.elems.add(new Prim(id)); 389 390 edge = null; 391 osm = S57osm.OSMtag("", ""); 390 392 } 391 393 … … 396 398 feature.geom.elems.add(new Prim(id)); 397 399 edge = new Edge(); 400 osm = S57osm.OSMtag("", ""); 398 401 } 399 402 … … 415 418 feature.geom.prim = Pflag.AREA; 416 419 edge = null; 420 osm = S57osm.OSMtag("", ""); 417 421 } 418 422 … … 474 478 } 475 479 } else { 476 KeyVal kv = S57osm.OSMtag(key, val);480 KeyVal<?> kv = S57osm.OSMtag(key, val); 477 481 if (kv.obj != Obj.UNKOBJ) { 478 if (feature.type == Obj.UNKOBJ) { 479 feature.type = kv.obj; 480 } 481 ObjTab objs = feature.objs.get(kv.obj); 482 if (objs == null) { 483 objs = new ObjTab(); 484 feature.objs.put(kv.obj, objs); 485 } 486 AttMap atts = objs.get(0); 487 if (atts == null) { 488 atts = new AttMap(); 489 objs.put(0, atts); 490 } 482 osm.obj = kv.obj; 491 483 if (kv.att != Att.UNKATT) { 492 atts.put(kv.att, new AttVal(kv.att, kv.conv, kv.val));484 osm = kv; 493 485 } 494 486 } … … 517 509 break; 518 510 } 519 if ((sortGeom(feature)) && (feature.type != Obj.UNKOBJ) && !((edge != null) && (edge.last == 0))) { 520 index.put(id, feature); 521 if (features.get(feature.type) == null) { 522 features.put(feature.type, new ArrayList<Feature>()); 523 } 524 features.get(feature.type).add(feature); 525 feature.geom.centre = findCentroid(feature); 526 } 527 } 528 511 if (sortGeom(feature) && !((edge != null) && (edge.last == 0))) { 512 if (osm.obj != Obj.UNKOBJ) { 513 if (feature.type == Obj.UNKOBJ) { 514 feature.type = osm.obj; 515 ObjTab objs = feature.objs.get(osm.obj); 516 if (objs == null) { 517 objs = new ObjTab(); 518 feature.objs.put(osm.obj, objs); 519 } 520 AttMap atts = objs.get(0); 521 if (atts == null) { 522 atts = new AttMap(); 523 objs.put(0, atts); 524 } 525 if (osm.att != Att.UNKATT) { 526 atts.put(osm.att, new AttVal<>(osm.att, osm.conv, osm.val)); 527 } 528 } else { 529 Feature base = new Feature(); 530 base.reln = Rflag.MASTER; 531 base.geom = feature.geom; 532 base.type = osm.obj; 533 ObjTab objs = new ObjTab(); 534 base.objs.put(osm.obj, objs); 535 AttMap atts = new AttMap(); 536 objs.put(0, atts); 537 if (osm.att != Att.UNKATT) { 538 atts.put(osm.att, new AttVal<>(osm.att, osm.conv, osm.val)); 539 } 540 index.put(++xref, base); 541 if (features.get(osm.obj) == null) { 542 features.put(osm.obj, new ArrayList<Feature>()); 543 } 544 features.get(osm.obj).add(base); 545 } 546 } 547 if (feature.type != Obj.UNKOBJ) { 548 index.put(id, feature); 549 if (features.get(feature.type) == null) { 550 features.put(feature.type, new ArrayList<Feature>()); 551 } 552 features.get(feature.type).add(feature); 553 } 554 } 555 } 556 557 enum Ext {I, N, W, S, E } 529 558 public void mapDone() { 530 ArrayList<Feature> coasts = new ArrayList<Feature>(); 531 ArrayList<Feature> lands = new ArrayList<Feature>(); 532 if (features.get(Obj.LNDARE) == null) { 533 features.put(Obj.LNDARE, new ArrayList<Feature>()); 534 } 535 for (Feature feature : features.get(Obj.COALNE)) { 536 Feature land = new Feature(); 537 land.type = Obj.LNDARE; 538 land.reln = Rflag.MASTER; 539 land.objs.put(Obj.LNDARE, new ObjTab()); 540 if (feature.geom.prim == Pflag.AREA) { 541 land.geom = feature.geom; 542 features.get(Obj.LNDARE).add(land); 543 } else if (feature.geom.prim == Pflag.LINE) { 544 land.geom.prim = Pflag.LINE; 545 for (int i = 0; i < feature.geom.elems.size(); i++) { 546 land.geom.elems.add(feature.geom.elems.get(i)); 547 } 548 coasts.add(land); 549 } 550 } 551 while (coasts.size() > 0) { 552 Feature land = coasts.remove(0); 553 Edge fedge = edges.get(land.geom.elems.get(0).id); 554 long first = fedge.first; 555 long last = edges.get(land.geom.elems.get(land.geom.elems.size() - 1).id).last; 556 if (coasts.size() > 0) { 557 boolean added = true; 558 while (added) { 559 added = false; 560 for (int i = 0; i < coasts.size(); i++) { 561 Feature coast = coasts.get(i); 562 Edge edge = edges.get(coast.geom.elems.get(0).id); 563 if (edge.first == last) { 564 land.geom.elems.add(coast.geom.elems.get(0)); 565 last = edge.last; 566 coasts.remove(i--); 567 added = true; 568 } else if (edge.last == first) { 569 land.geom.elems.add(0, coast.geom.elems.get(0)); 570 first = edge.first; 571 coasts.remove(i--); 572 added = true; 573 } 574 } 575 } 576 } 577 lands.add(land); 578 } 579 for (Feature land : lands) { 580 long first = edges.get(land.geom.elems.get(0).id).first; 581 long last = edges.get(land.geom.elems.get(land.geom.elems.size()-1).id).last; 582 Ext fext = outsideBounds(first); 583 Ext lext = outsideBounds(last); 584 Edge nedge = new Edge(); 585 nedge.first = last; 586 nedge.last = first; 587 switch (lext) { 588 case N: 589 if ((lext == fext) || (fext != Ext.N)) { 590 nedge.nodes.add(1L); 591 if ((fext != Ext.W)) { 592 nedge.nodes.add(2L); 593 if ((fext != Ext.S)) { 594 nedge.nodes.add(3L); 595 if ((fext != Ext.W)) { 596 nedge.nodes.add(4L); 559 class Land { 560 long first; 561 Snode start; 562 Ext sbound; 563 long last; 564 Snode end; 565 Ext ebound; 566 Feature land; 567 Land (Feature l) { 568 land = l; 569 first = last = 0; 570 start = end = null; 571 sbound = ebound = Ext.I; 572 } 573 } 574 if (features.get(Obj.COALNE) != null) { 575 ArrayList<Feature> coasts = new ArrayList<Feature>(); 576 ArrayList<Land> lands = new ArrayList<Land>(); 577 if (features.get(Obj.LNDARE) == null) { 578 features.put(Obj.LNDARE, new ArrayList<Feature>()); 579 } 580 for (Feature feature : features.get(Obj.COALNE)) { 581 Feature land = new Feature(); 582 land.type = Obj.LNDARE; 583 land.reln = Rflag.MASTER; 584 land.objs.put(Obj.LNDARE, new ObjTab()); 585 if (feature.geom.prim == Pflag.AREA) { 586 land.geom = feature.geom; 587 features.get(Obj.LNDARE).add(land); 588 } else if (feature.geom.prim == Pflag.LINE) { 589 land.geom.prim = Pflag.LINE; 590 for (int i = 0; i < feature.geom.elems.size(); i++) { 591 land.geom.elems.add(feature.geom.elems.get(i)); 592 } 593 coasts.add(land); 594 } 595 } 596 while (coasts.size() > 0) { 597 Feature land = coasts.remove(0); 598 Edge fedge = edges.get(land.geom.elems.get(0).id); 599 long first = fedge.first; 600 long last = edges.get(land.geom.elems.get(land.geom.elems.size() - 1).id).last; 601 if (coasts.size() > 0) { 602 boolean added = true; 603 while (added) { 604 added = false; 605 for (int i = 0; i < coasts.size(); i++) { 606 Feature coast = coasts.get(i); 607 Edge edge = edges.get(coast.geom.elems.get(0).id); 608 if (edge.first == last) { 609 land.geom.elems.add(coast.geom.elems.get(0)); 610 last = edge.last; 611 coasts.remove(i--); 612 added = true; 613 } else if (edge.last == first) { 614 land.geom.elems.add(0, coast.geom.elems.get(0)); 615 first = edge.first; 616 coasts.remove(i--); 617 added = true; 597 618 } 598 619 } 599 620 } 600 621 } 601 break; 602 case W: 603 if ((lext == fext) || (fext != Ext.W)) { 604 nedge.nodes.add(2L); 605 if ((fext != Ext.S)) { 606 nedge.nodes.add(3L); 607 if ((fext != Ext.E)) { 608 nedge.nodes.add(4L); 609 if ( (fext != Ext.N)) { 610 nedge.nodes.add(1L); 622 lands.add(new Land(land)); 623 } 624 ArrayList<Land> islands = new ArrayList<Land>(); 625 for (Land land : lands) { 626 sortGeom(land.land); 627 if (land.land.geom.prim == Pflag.AREA) { 628 islands.add(land); 629 features.get(Obj.LNDARE).add(land.land); 630 } 631 } 632 for (Land island : islands) { 633 lands.remove(island); 634 } 635 for (Land land : lands) { 636 GeomIterator git = new GeomIterator(land.land.geom); 637 Snode prev = null; 638 Ext bprev = Ext.I; 639 Ext ext; 640 land.ebound = land.sbound = Ext.I; 641 while (git.hasComp()) { 642 git.nextComp(); 643 while (git.hasEdge()) { 644 git.nextEdge(); 645 while (git.hasNode()) { 646 long ref = git.nextRef(false); 647 Snode node = nodes.get(ref); 648 if (node == null) continue; 649 if (land.first == 0) { 650 land.first = ref; 651 } 652 if (prev == null) { 653 prev = node; 654 } 655 if ((node.lat >= bounds.maxlat) && (node.lon < bounds.maxlon)) { 656 ext = Ext.N; 657 } else if (node.lon <= bounds.minlon) { 658 ext = Ext.W; 659 } else if (node.lat <= bounds.minlat) { 660 ext = Ext.S; 661 } else if (node.lon >= bounds.maxlon) { 662 ext = Ext.E; 663 } else { 664 ext = Ext.I; 665 } 666 if (ext == Ext.I) { 667 if (land.start == null) { 668 land.start = prev; 669 land.sbound = bprev; 670 } 671 land.end = null; 672 land.ebound = Ext.I; 673 } else { 674 if ((land.start != null) && (land.end == null)) { 675 land.end = node; 676 land.ebound = ext; 677 } 678 } 679 prev = node; 680 bprev = ext; 681 land.last = ref; 682 } 683 } 684 } 685 } 686 islands = new ArrayList<Land>(); 687 for (Land land : lands) { 688 if ((land.sbound == Ext.I) || (land.ebound == Ext.I)) { 689 islands.add(land); 690 } 691 } 692 for (Land island : islands) { 693 lands.remove(island); 694 } 695 while (lands.size() > 0) { 696 Land land = lands.get(0); 697 Edge nedge = new Edge(); 698 nedge.first = land.last; 699 Ext bound = land.ebound; 700 Snode last = land.end; 701 double delta = Math.PI; 702 int idx = -1; 703 Land next = null; 704 while (idx < 0) { 705 for (int i = 0; i < lands.size(); i++) { 706 next = lands.get(i); 707 if (next.sbound == bound) { 708 double diff = -Math.PI; 709 switch (bound) { 710 case N: 711 diff = last.lon - next.start.lon; 712 break; 713 case W: 714 diff = last.lat - next.start.lat; 715 break; 716 case S: 717 diff = next.start.lon - last.lon; 718 break; 719 case E: 720 diff = next.start.lat - last.lat; 721 break; 722 default: 723 continue; 724 } 725 if ((diff >= 0.0) && (diff < delta)) { 726 delta = diff; 727 idx = i; 611 728 } 612 729 } 613 730 } 614 } 615 break; 616 case S: 617 if ((lext == fext) || (fext != Ext.S)) { 618 nedge.nodes.add(3L); 619 if ((fext != Ext.E)) { 620 nedge.nodes.add(4L); 621 if ((fext != Ext.N)) { 622 nedge.nodes.add(1L); 623 if ((fext != Ext.W)) { 624 nedge.nodes.add(2L); 731 if (idx < 0) { 732 long ref = (long)bound.ordinal(); 733 last = nodes.get(ref); 734 nedge.nodes.add(ref); 735 ref = ref < 4 ? ++ref : 1; 736 for (Ext e : Ext.values()) { 737 if (ref == e.ordinal()) { 738 bound = e; 739 break; 625 740 } 626 741 } 627 742 } 628 743 } 629 break; 630 case E: 631 if ((lext == fext) || (fext != Ext.E)) { 632 nedge.nodes.add(4L); 633 if ((fext != Ext.N)) { 634 nedge.nodes.add(1L); 635 if ((fext != Ext.W)) { 636 nedge.nodes.add(2L); 637 if ((fext != Ext.S)) { 638 nedge.nodes.add(3L); 639 } 640 } 641 } 642 } 643 break; 644 default: 645 } 646 edges.put(++xref, nedge); 647 land.geom.elems.add(new Prim(xref)); 648 sortGeom(land); 649 features.get(Obj.LNDARE).add(land); 744 next = lands.get(idx); 745 nedge.last = next.first; 746 edges.put(++xref, nedge); 747 land.land.geom.elems.add(new Prim(xref)); 748 if (next != land) { 749 land.land.geom.elems.addAll(next.land.geom.elems); 750 land.ebound = next.ebound; 751 land.end = next.end; 752 land.last = next.last; 753 lands.remove(idx); 754 } 755 sortGeom(land.land); 756 if (land.land.geom.prim == Pflag.AREA) { 757 features.get(Obj.LNDARE).add(land.land); 758 lands.remove(land); 759 } 760 } 650 761 } 651 762 return; … … 653 764 654 765 // Utility methods 655 656 enum Ext {I, N, W, S, E }657 class Xnode {658 double lat;659 double lon;660 Ext ext;661 }662 Ext outsideBounds(long ref) {663 Snode node = nodes.get(ref);664 if (node.lat >= bounds.maxlat) {665 return Ext.N;666 }667 if (node.lat <= bounds.minlat) {668 return Ext.S;669 }670 if (node.lon >= bounds.maxlon) {671 return Ext.E;672 }673 if (node.lon <= bounds.minlon) {674 return Ext.W;675 }676 return Ext.I;677 }678 766 679 767 public boolean sortGeom(Feature feature) { … … 686 774 feature.geom.area = 0; 687 775 if (feature.geom.prim == Pflag.POINT) { 776 feature.geom.centre = findCentroid(feature); 688 777 return true; 689 778 } else { … … 697 786 if (next == true) { 698 787 next = false; 699 if (prim.forward) { 700 first = edge.first; 701 last = edge.last; 702 } else { 703 first = edge.last; 704 last = edge.first; 705 } 788 first = edge.first; 789 last = edge.last; 790 prim.forward = true; 706 791 sort.elems.add(prim); 707 792 if (prim.outer) { … … 711 796 } 712 797 comp = new Comp(cref++, 1); 713 sort. refs.add(comp);798 sort.comps.add(comp); 714 799 } else { 715 if (prim.forward) { 716 if (edge.first == last) { 717 sort.elems.add(prim); 718 last = edge.last; 719 comp.size++; 720 } else if (edge.last == first) { 721 sort.elems.add(0, prim); 722 first = edge.first; 723 comp.size++; 724 } else { 725 feature.geom.elems.add(prim); 726 } 800 if (edge.first == last) { 801 sort.elems.add(prim); 802 last = edge.last; 803 prim.forward = true; 804 comp.size++; 805 } else if (edge.last == first) { 806 sort.elems.add(0, prim); 807 first = edge.first; 808 prim.forward = true; 809 comp.size++; 810 } else if (edge.last == last) { 811 sort.elems.add(prim); 812 last = edge.first; 813 prim.forward = false; 814 comp.size++; 815 } else if (edge.first == first) { 816 sort.elems.add(0, prim); 817 first = edge.last; 818 prim.forward = false; 819 comp.size++; 727 820 } else { 728 if (edge.last == last) { 729 sort.elems.add(prim); 730 last = edge.first; 731 comp.size++; 732 } else if (edge.first == first) { 733 sort.elems.add(0, prim); 734 first = edge.last; 735 comp.size++; 736 } else { 737 feature.geom.elems.add(prim); 738 } 821 feature.geom.elems.add(prim); 739 822 } 740 823 } … … 750 833 } 751 834 if (feature.geom.prim == Pflag.AREA) { 752 ArrayList<Prim> outers = new ArrayList<Prim>(); 753 ArrayList<Prim> inners = new ArrayList<Prim>(); 754 for (Prim prim : feature.geom.elems) { 755 if (prim.outer) { 756 outers.add(prim); 757 } else { 758 inners.add(prim); 759 } 760 } 761 ArrayList<Prim> sorting = outers.size() > 0 ? outers : inners; 762 ArrayList<Prim> closed = null; 763 sort = new Geom(feature.geom.prim); 764 sort.outers = feature.geom.outers; 765 sort.inners = feature.geom.inners; 766 sort.refs = feature.geom.refs; 767 next = true; 768 while (!sorting.isEmpty()) { 769 Prim prim = sorting.remove(0); 770 Edge edge = edges.get(prim.id); 771 if (next == true) { 772 next = false; 773 closed = new ArrayList<Prim>(); 774 closed.add(prim); 775 if (prim.forward) { 776 first = edge.first; 777 last = edge.last; 778 } else { 779 first = edge.last; 780 last = edge.first; 781 } 782 } else { 783 if (prim.forward) { 784 if (edge.first == last) { 785 last = edge.last; 786 closed.add(prim); 787 } else { 788 sorting.add(0, prim); 789 next = true; 790 } 791 } else { 792 if (edge.last == last) { 793 last = edge.first; 794 closed.add(prim); 795 } else { 796 sorting.add(0, prim); 797 next = true; 798 } 799 } 800 } 801 if (first == last) { 802 sort.elems.addAll(closed); 803 next = true; 804 } 805 if (sorting.isEmpty() && sorting == outers) { 806 sorting = inners; 807 next = true; 808 } 809 } 810 feature.geom = sort; 835 if (signedArea(feature.geom) < 0.0) { 836 837 }; 838 feature.geom.area = calcArea(feature.geom); 811 839 } 812 840 feature.geom.length = calcLength(feature.geom); 813 feature.geom. area = calcArea(feature.geom);841 feature.geom.centre = findCentroid(feature); 814 842 return true; 815 843 } … … 883 911 lastref = 0; 884 912 ite = geom.elems.listIterator(); 885 itc = geom. refs.listIterator();913 itc = geom.comps.listIterator(); 886 914 } 887 915
Note:
See TracChangeset
for help on using the changeset viewer.