Changeset 20243 in osm for applications/editors


Ignore:
Timestamp:
2010-03-02T01:59:22+01:00 (14 years ago)
Author:
tordanik
Message:

add, use and test measure and weight parsing methods

Location:
applications/editors/josm/plugins/graphview/src/org/openstreetmap/josm/plugins/graphview
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/graphview/src/org/openstreetmap/josm/plugins/graphview/core/property/RoadMaxaxleload.java

    r16520 r20243  
    22
    33import static org.openstreetmap.josm.plugins.graphview.core.property.VehiclePropertyTypes.AXLELOAD;
     4
     5import org.openstreetmap.josm.plugins.graphview.core.util.ValueStringParser;
    46
    57public class RoadMaxaxleload extends RoadValueLimit {
     
    79                super("maxaxleload", AXLELOAD, LimitType.MAXIMUM);
    810        }
     11        @Override
     12        protected Float parse(String valueString) {
     13                return ValueStringParser.parseWeight(valueString);
     14        }
    915}
  • applications/editors/josm/plugins/graphview/src/org/openstreetmap/josm/plugins/graphview/core/property/RoadMaxheight.java

    r16520 r20243  
    22
    33import static org.openstreetmap.josm.plugins.graphview.core.property.VehiclePropertyTypes.HEIGHT;
     4
     5import org.openstreetmap.josm.plugins.graphview.core.util.ValueStringParser;
    46
    57public class RoadMaxheight extends RoadValueLimit {
     
    79                super("maxheight", HEIGHT, LimitType.MAXIMUM);
    810        }
     11        @Override
     12        protected Float parse(String valueString) {
     13                return ValueStringParser.parseMeasure(valueString);
     14        }
    915}
  • applications/editors/josm/plugins/graphview/src/org/openstreetmap/josm/plugins/graphview/core/property/RoadMaxlength.java

    r16520 r20243  
    22
    33import static org.openstreetmap.josm.plugins.graphview.core.property.VehiclePropertyTypes.LENGTH;
     4
     5import org.openstreetmap.josm.plugins.graphview.core.util.ValueStringParser;
    46
    57public class RoadMaxlength extends RoadValueLimit {
     
    79                super("maxlength", LENGTH, LimitType.MAXIMUM);
    810        }
     11        @Override
     12        protected Float parse(String valueString) {
     13                return ValueStringParser.parseMeasure(valueString);
     14        }
    915}
  • applications/editors/josm/plugins/graphview/src/org/openstreetmap/josm/plugins/graphview/core/property/RoadMaxweight.java

    r16520 r20243  
    22
    33import static org.openstreetmap.josm.plugins.graphview.core.property.VehiclePropertyTypes.WEIGHT;
     4
     5import org.openstreetmap.josm.plugins.graphview.core.util.ValueStringParser;
    46
    57public class RoadMaxweight extends RoadValueLimit {
     
    79                super("maxweight", WEIGHT, LimitType.MAXIMUM);
    810        }
     11        @Override
     12        protected Float parse(String valueString) {
     13                return ValueStringParser.parseWeight(valueString);
     14        }
    915}
  • applications/editors/josm/plugins/graphview/src/org/openstreetmap/josm/plugins/graphview/core/property/RoadMaxwidth.java

    r16520 r20243  
    22
    33import static org.openstreetmap.josm.plugins.graphview.core.property.VehiclePropertyTypes.WIDTH;
     4
     5import org.openstreetmap.josm.plugins.graphview.core.util.ValueStringParser;
    46
    57public class RoadMaxwidth extends RoadValueLimit {
     
    79                super("maxwidth", WIDTH, LimitType.MAXIMUM);
    810        }
     11        @Override
     12        protected Float parse(String valueString) {
     13                return ValueStringParser.parseMeasure(valueString);
     14        }
    915}
  • applications/editors/josm/plugins/graphview/src/org/openstreetmap/josm/plugins/graphview/core/property/RoadMinspeed.java

    r16520 r20243  
    22
    33import static org.openstreetmap.josm.plugins.graphview.core.property.VehiclePropertyTypes.SPEED;
     4
     5import org.openstreetmap.josm.plugins.graphview.core.util.ValueStringParser;
    46
    57public class RoadMinspeed extends RoadValueLimit {
     
    79                super("minspeed", SPEED, LimitType.MINIMUM);
    810        }
     11        @Override
     12        protected Float parse(String valueString) {
     13                return ValueStringParser.parseSpeed(valueString);
     14        }
    915}
  • applications/editors/josm/plugins/graphview/src/org/openstreetmap/josm/plugins/graphview/core/property/RoadValueLimit.java

    r19216 r20243  
    3434        }
    3535
     36        protected abstract Float parse(String valueString);
     37
    3638        public <N, W, R, M> Float evaluateW(W way, boolean forward,
    3739                        AccessParameters accessParameters, DataSource<N, W, R, M> dataSource) {
     
    4951                String valueString = tags.getValue(keyName);
    5052                if (valueString != null) {
    51                         Float value = ValueStringParser.parseOsmDecimal(valueString, false);
     53                        Float value = parse(valueString);
    5254                        return value;
    5355                } else {
  • applications/editors/josm/plugins/graphview/src/org/openstreetmap/josm/plugins/graphview/core/property/RoadWidth.java

    r16520 r20243  
    22
    33import static org.openstreetmap.josm.plugins.graphview.core.property.VehiclePropertyTypes.WIDTH;
     4
     5import org.openstreetmap.josm.plugins.graphview.core.util.ValueStringParser;
    46
    57public class RoadWidth extends RoadValueLimit {
     
    79                super("width", WIDTH, LimitType.MAXIMUM);
    810        }
     11        @Override
     12        protected Float parse(String valueString) {
     13                return ValueStringParser.parseMeasure(valueString);
     14        }
    915}
  • applications/editors/josm/plugins/graphview/src/org/openstreetmap/josm/plugins/graphview/core/util/ValueStringParser.java

    r16520 r20243  
    7171                /* try numeric speed (implied km/h) */
    7272
    73                 Float maxspeed = parseOsmDecimal(value, false);
    74                 if (maxspeed != null) {
    75                         return maxspeed;
     73                Float speed = parseOsmDecimal(value, false);
     74                if (speed != null) {
     75                        return speed;
    7676                }
    7777
     
    102102        }
    103103
     104        private static final Pattern M_PATTERN = Pattern.compile("^([\\d\\.]+)\\s*m$");
     105        private static final Pattern KM_PATTERN = Pattern.compile("^([\\d\\.]+)\\s*km$");
     106        private static final Pattern MI_PATTERN = Pattern.compile("^([\\d\\.]+)\\s*mi$");
     107        private static final Pattern FEET_INCHES_PATTERN = Pattern.compile("^([\\d]+)'\\s*([\\d]+)\"");
     108
     109        private static final double M_PER_MI = 1609.344;
     110        private static final double M_PER_INCH = 0.0254f;
     111
     112        /**
     113         * parses a measure value given e.g. for the "width" or "length" key.
     114         *
     115         * @return  measure in m; null if value had syntax errors
     116         */
     117        public static final Float parseMeasure(String value) {
     118
     119                /* try numeric measure (implied m) */
     120
     121                Float measure = parseOsmDecimal(value, false);
     122                if (measure != null) {
     123                        return measure;
     124                }
     125
     126                /* try m measure */
     127
     128                Matcher mMatcher = M_PATTERN.matcher(value);
     129                if (mMatcher.matches()) {
     130                        String mString = mMatcher.group(1);
     131                        return parseOsmDecimal(mString, false);
     132                }
     133
     134                /* try km measure */
     135
     136                Matcher kmMatcher = KM_PATTERN.matcher(value);
     137                if (kmMatcher.matches()) {
     138                        String kmString = kmMatcher.group(1);
     139                        float km = parseOsmDecimal(kmString, false);
     140                        return 1000 * km;
     141                }
     142
     143                /* try mi measure */
     144
     145                Matcher miMatcher = MI_PATTERN.matcher(value);
     146                if (miMatcher.matches()) {
     147                        String miString = miMatcher.group(1);
     148                        float mi = parseOsmDecimal(miString, false);
     149                        return (float)(M_PER_MI * mi);
     150                }
     151
     152                /* try feet/inches measure */
     153
     154                Matcher feetInchesMatcher = FEET_INCHES_PATTERN.matcher(value);
     155                if (feetInchesMatcher.matches()) {
     156                        String feetString = feetInchesMatcher.group(1);
     157                        String inchesString = feetInchesMatcher.group(2);
     158                        try {
     159                                int feet = Integer.parseInt(feetString);
     160                                int inches = Integer.parseInt(inchesString);
     161                                if (feet >= 0 && inches >= 0 && inches < 12) {
     162                                        return (float)(M_PER_INCH * (12 * feet + inches));
     163                                }
     164                        } catch (NumberFormatException nfe) {}
     165                }
     166
     167                /* all possibilities failed */
     168
     169                return null;
     170        }
     171
     172        private static final Pattern T_PATTERN = Pattern.compile("^([\\d\\.]+)\\s*t$");
     173
     174        /**
     175         * parses a weight value given e.g. for the "maxweight" or "maxaxleload" key.
     176         *
     177         * @return  weight in t; null if value had syntax errors
     178         */
     179        public static Float parseWeight(String value) {
     180
     181                /* try numeric weight (implied t) */
     182
     183                Float weight = parseOsmDecimal(value, false);
     184                if (weight != null) {
     185                        return weight;
     186                }
     187
     188                /* try t weight */
     189
     190                Matcher tMatcher = T_PATTERN.matcher(value);
     191                if (tMatcher.matches()) {
     192                        String tString = tMatcher.group(1);
     193                        return parseOsmDecimal(tString, false);
     194                }
     195
     196                /* all possibilities failed */
     197
     198                return null;
     199
     200        }
     201
    104202        private static final Pattern INCLINE_PATTERN = Pattern.compile("^(\\-?\\d+(?:\\.\\d+)?)\\s*%$");
    105203
     
    119217                return null;
    120218        }
     219
    121220}
  • applications/editors/josm/plugins/graphview/src/org/openstreetmap/josm/plugins/graphview/plugin/preferences/VehiclePropertyStringParser.java

    r16520 r20243  
    1515 */
    1616public final class VehiclePropertyStringParser {
    17        
     17
    1818        /** prevents instantiation */
    1919        private VehiclePropertyStringParser() { }
    20        
     20
    2121        /**
    2222         * Exception class for syntax errors in property value Strings,
     
    2929                }
    3030        }
    31        
     31
    3232        public static final String ERROR_WEIGHT =
    33                 "Weights must be given as positive decimal numbers without unit.";
     33                "Weights must be given as positive decimal numbers with unit \"t\" or without unit.";
    3434        public static final String ERROR_LENGTH =
    35                 "Lengths must be given as positive decimal numbers without unit.";
     35                "Lengths must be given as positive decimal numbers with unit \"m\", \"km\", \"mi\"" +
     36                " or without unit.\nAlternatively, the format FEET' INCHES\" can be used.";
    3637        public static final String ERROR_SPEED =
    3738                "Speeds should be given as numbers without unit or "
     
    4344        public static final String ERROR_SURFACE =
    4445                "Surface values must not contain any of the following characters: ',' '{' '}' '=' '|";
    45        
     46
    4647        private static final List<Character> FORBIDDEN_SURFACE_CHARS =
    4748                Arrays.asList(',', '{', '}', '=', '|');
    48        
     49
    4950        /**
    5051         * returns the value represented by the propertyValueString
     
    6263                        VehiclePropertyType<V> propertyType, String propertyValueString)
    6364        throws PropertyValueSyntaxException {
    64                
     65
    6566                assert propertyType != null && propertyValueString != null;
    66                
     67
    6768                if (propertyType == VehiclePropertyTypes.AXLELOAD
    6869                                || propertyType == VehiclePropertyTypes.WEIGHT) {
    69                        
    70                         Float value = ValueStringParser.parseOsmDecimal(propertyValueString, false);
     70
     71                        Float value = ValueStringParser.parseWeight(propertyValueString);
    7172                        if (value != null && propertyType.isValidValue(value)) {
    7273                                @SuppressWarnings("unchecked") //V must be float because of propertyType condition
     
    7677                                throw new PropertyValueSyntaxException(ERROR_WEIGHT);
    7778                        }
    78                        
     79
    7980                } else if (propertyType == VehiclePropertyTypes.HEIGHT
    8081                                || propertyType == VehiclePropertyTypes.LENGTH
    8182                                || propertyType == VehiclePropertyTypes.WIDTH) {
    82                        
    83                         Float value = ValueStringParser.parseOsmDecimal(propertyValueString, false);
     83
     84                        Float value = ValueStringParser.parseMeasure(propertyValueString);
    8485                        if (value != null && propertyType.isValidValue(value)) {
    8586                                @SuppressWarnings("unchecked") //V must be float because of propertyType condition
     
    8990                                throw new PropertyValueSyntaxException(ERROR_LENGTH);
    9091                        }
    91                        
     92
    9293                } else if (propertyType == VehiclePropertyTypes.SPEED) {
    93                        
     94
    9495                        Float value = ValueStringParser.parseSpeed(propertyValueString);
    9596                        if (value != null && propertyType.isValidValue(value)) {
     
    100101                                throw new PropertyValueSyntaxException(ERROR_SPEED);
    101102                        }
    102                        
     103
    103104                } else if (propertyType == VehiclePropertyTypes.MAX_INCLINE_DOWN
    104105                                || propertyType == VehiclePropertyTypes.MAX_INCLINE_UP) {
    105                        
     106
    106107                        Float value = ValueStringParser.parseIncline(propertyValueString);
    107108                        if (value != null && propertyType.isValidValue(value)) {
     
    112113                                throw new PropertyValueSyntaxException(ERROR_INCLINE);
    113114                        }
    114                        
     115
    115116                } else if (propertyType == VehiclePropertyTypes.MAX_TRACKTYPE) {
    116                        
     117
    117118                        try {
    118119                                int value = Integer.parseInt(propertyValueString);
     
    123124                                }
    124125                        } catch (NumberFormatException e) {}
    125                        
     126
    126127                        throw new PropertyValueSyntaxException(ERROR_TRACKTYPE);
    127                        
     128
    128129                } else if (propertyType == VehiclePropertyTypes.SURFACE_BLACKLIST) {
    129                        
     130
    130131                        String[] surfaces = propertyValueString.split(";\\s*");
    131132                        Collection<String> surfaceBlacklist = new ArrayList<String>(surfaces.length);
     
    138139                                surfaceBlacklist.add(surface);
    139140                        }
    140                        
     141
    141142                        @SuppressWarnings("unchecked") //V must be Collection because of propertyType condition
    142143                        V result = (V)surfaceBlacklist;
    143144                        return result;
    144                        
     145
    145146                } else {
    146147                        throw new InvalidParameterException("unknown property type: " + propertyType);
    147148                }
    148                
     149
    149150        }
    150        
     151
    151152}
Note: See TracChangeset for help on using the changeset viewer.