comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java @ 1095:f465785ed1ae

Refactored the code to fetch the km range/locations into a utility class. flys-artifacts/trunk@2598 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 26 Aug 2011 14:58:35 +0000
parents 61c051e53f9b
children 7bd514eb4b2c
comparison
equal deleted inserted replaced
1094:b1c6d945848d 1095:f465785ed1ae
38 38
39 import de.intevation.flys.model.Gauge; 39 import de.intevation.flys.model.Gauge;
40 import de.intevation.flys.model.River; 40 import de.intevation.flys.model.River;
41 41
42 import de.intevation.flys.utils.DoubleUtil; 42 import de.intevation.flys.utils.DoubleUtil;
43 import de.intevation.flys.utils.FLYSUtils;
43 44
44 import gnu.trove.TDoubleArrayList; 45 import gnu.trove.TDoubleArrayList;
45 46
46 import java.util.ArrayList; 47 import java.util.ArrayList;
47 import java.util.Arrays; 48 import java.util.Arrays;
319 if (wst == null) { 320 if (wst == null) {
320 return error(new WQKms[0], "No Wst found for selected river."); 321 return error(new WQKms[0], "No Wst found for selected river.");
321 } 322 }
322 323
323 324
324 double [] range = getDistance(); 325 double [] range = FLYSUtils.getKmRange(this);
325 if (range == null) { 326 if (range == null) {
326 return error(new WQKms[0], "No range found"); 327 return error(new WQKms[0], "No range found");
327 } 328 }
328 329
329 double refKm; 330 double refKm;
391 392
392 if (g == null) { 393 if (g == null) {
393 return error(null, "Cannot determine gauge."); 394 return error(null, "Cannot determine gauge.");
394 } 395 }
395 396
396 double[] locations = getLocations(); 397 double[] locations = FLYSUtils.getLocations(this);
397 398
398 if (locations == null) { 399 if (locations == null) {
399 return error(null, "Cannot determine location."); 400 return error(null, "Cannot determine location.");
400 } 401 }
401 402
443 River river = getRiver(); 444 River river = getRiver();
444 if (river == null) { 445 if (river == null) {
445 return error(new WQKms[0], "no river found"); 446 return error(new WQKms[0], "no river found");
446 } 447 }
447 448
448 double [] distance = getDistance(); 449 double [] distance = FLYSUtils.getKmRange(this);
449 450
450 if (distance == null) { 451 if (distance == null) {
451 return error(new WQKms[0], "no range found"); 452 return error(new WQKms[0], "no range found");
452 } 453 }
453 454
500 501
501 if (r == null) { 502 if (r == null) {
502 return error(new WQKms[0], "Cannot determine river."); 503 return error(new WQKms[0], "Cannot determine river.");
503 } 504 }
504 505
505 double[] locations = getLocations(); 506 double[] locations = FLYSUtils.getLocations(this);
506 507
507 if (locations == null) { 508 if (locations == null) {
508 return error(new WQKms[0], "Cannot determine location."); 509 return error(new WQKms[0], "Cannot determine location.");
509 } 510 }
510 511
617 if (r == null) { 618 if (r == null) {
618 logger.warn("no river found"); 619 logger.warn("no river found");
619 return null; 620 return null;
620 } 621 }
621 622
622 double [] range = getDistance(); 623 double [] range = FLYSUtils.getKmRange(this);
623 if (range == null) { 624 if (range == null) {
624 logger.warn("no ranges found"); 625 logger.warn("no ranges found");
625 return null; 626 return null;
626 } 627 }
627 628
667 return dRiver != null 668 return dRiver != null
668 ? RiverFactory.getRiver((String) dRiver.getValue()) 669 ? RiverFactory.getRiver((String) dRiver.getValue())
669 : null; 670 : null;
670 } 671 }
671 672
672
673 /**
674 * Returns the selected distance of points.
675 *
676 * @return the selected distance or points.
677 */
678 public double[] getDistance() {
679 StateData dMode = getData("ld_mode");
680 StateData dFrom = getData("ld_from");
681 StateData dTo = getData("ld_to");
682 StateData dLocations = getData("ld_locations");
683
684 if (dMode != null) {
685 String mode = (String)dMode.getValue();
686 if ("location".equals(mode)) {
687 double[] locations = getLocations();
688 return new double[] { locations[0], locations[locations.length-1] };
689 }
690 if (dFrom != null && dTo != null) {
691 return getDistanceByRange(dFrom, dTo);
692 }
693 }
694
695 if (dLocations != null) {
696 double[] locations = getLocations();
697 return new double[] { locations[0], locations[locations.length-1] };
698 }
699
700 if (dFrom != null && dTo != null) {
701 return getDistanceByRange(dFrom, dTo);
702 }
703
704 logger.warn("No data found for distance determination!");
705
706 return null;
707 }
708
709
710 /** 673 /**
711 * Determines the selected mode of distance/range input. 674 * Determines the selected mode of distance/range input.
712 * 675 *
713 * @return true, if the range mode is selected otherwise false. 676 * @return true, if the range mode is selected otherwise false.
714 */ 677 */
725 return value.equals("distance"); 688 return value.equals("distance");
726 } 689 }
727 690
728 691
729 /** 692 /**
730 * Returns the selected locations based on a given array of locations.
731 *
732 * @param locations The StateData that contains the locations.
733 *
734 * @return the selected locations.
735 */
736 public double[] getLocations() {
737 StateData dLocations = getData("ld_locations");
738 String locationStr = dLocations != null
739 ? (String) dLocations.getValue()
740 : "";
741
742 if (locationStr == null || locationStr.length() == 0) {
743 logger.warn("No valid location string found!");
744 return null;
745 }
746
747 String[] tmp = locationStr.split(" ");
748 TDoubleArrayList locations = new TDoubleArrayList();
749
750 for (String l: tmp) {
751 try {
752 locations.add(Double.parseDouble(l));
753 }
754 catch (NumberFormatException nfe) {
755 logger.warn(nfe, nfe);
756 }
757 }
758
759 locations.sort();
760
761 return locations.toNativeArray();
762 }
763
764
765 /**
766 * Returns the selected distance based on a given range (from, to). 693 * Returns the selected distance based on a given range (from, to).
767 * 694 *
768 * @param dFrom The StateData that contains the lower value. 695 * @param dFrom The StateData that contains the lower value.
769 * @param dTo The StateData that contains the upper value. 696 * @param dTo The StateData that contains the upper value.
770 * 697 *
811 * 738 *
812 * @return the selected kms. 739 * @return the selected kms.
813 */ 740 */
814 public double[] getKms() { 741 public double[] getKms() {
815 if (isRange()) { 742 if (isRange()) {
816 double[] distance = getDistance(); 743 double[] distance = FLYSUtils.getKmRange(this);
817 return getKms(distance); 744 return getKms(distance);
818 745
819 } 746 }
820 else { 747 else {
821 return LocationDistanceSelect.getLocations(this); 748 return LocationDistanceSelect.getLocations(this);
824 751
825 public double [] getFromToStep() { 752 public double [] getFromToStep() {
826 if (!isRange()) { 753 if (!isRange()) {
827 return null; 754 return null;
828 } 755 }
829 double [] fromTo = getDistance(); 756 double [] fromTo = FLYSUtils.getKmRange(this);
830 757
831 if (fromTo == null) { 758 if (fromTo == null) {
832 return null; 759 return null;
833 } 760 }
834 761
864 if (river == null) { 791 if (river == null) {
865 logger.debug("no river found"); 792 logger.debug("no river found");
866 return null; 793 return null;
867 } 794 }
868 795
869 double[] dist = getDistance(); 796 double[] dist = FLYSUtils.getKmRange(this);
870 797
871 if (dist == null) { 798 if (dist == null) {
872 logger.debug("no range found"); 799 logger.debug("no range found");
873 return null; 800 return null;
874 } 801 }
898 River river = getRiver(); 825 River river = getRiver();
899 if (river == null) { 826 if (river == null) {
900 return null; 827 return null;
901 } 828 }
902 829
903 double [] dist = getDistance(); 830 double [] dist = FLYSUtils.getKmRange(this);
904 if (dist == null) { 831 if (dist == null) {
905 return null; 832 return null;
906 } 833 }
907 834
908 return river.determineGauges(dist[0], dist[1]); 835 return river.determineGauges(dist[0], dist[1]);

http://dive4elements.wald.intevation.org