logo separator

[mkgmap-dev] [PATCH] Add a substring filter to the style language.

From Jeffrey Ollie jeff at ocjtech.us on Wed Apr 21 14:12:45 BST 2010

Has anyone had a chance to try this out?  It would be nice to get this
committed if there aren't any issues with it...

On Mon, Mar 15, 2010 at 11:51 AM, Jeffrey C. Ollie <jeff at ocjtech.us> wrote:
> This can be used to extract substrings from values.  If the "name" tag on an object was
> equal to "abcdef":
>
> '${name|substring:2}' => 'cdef'
> '${name|substring:2:4}' => 'cd'
>
> As an example, it can be used to clean up the "ref" element on route relations:
>
> type=route & route=road & network=US:I & ref ~ 'I\d+' { apply { set route_network='US:I'; set route_ref='${ref|substring:1}'; } }
> type=route & route=road & network=US:I & ref ~ 'I \d+' { apply { set route_network='US:I'; set route_ref='${ref|substring:2}'; } }
> type=route & route=road & network=US:I & ref ~ 'I-\d+' { apply { set route_network='US:I'; set route_ref='${ref|substring:2}'; } }
> type=route & route=road & network=US:I & ref ~ '\d+' { apply { set route_network='US:I'; set route_ref='${ref}'; } }
> ---
>  .../mkgmap/osmstyle/actions/SubstringFilter.java   |   66 ++++++++++++++++++++
>  .../mkgmap/osmstyle/actions/ValueBuilder.java      |    2 +
>  2 files changed, 68 insertions(+), 0 deletions(-)
>  create mode 100644 src/uk/me/parabola/mkgmap/osmstyle/actions/SubstringFilter.java
>
> diff --git a/src/uk/me/parabola/mkgmap/osmstyle/actions/SubstringFilter.java b/src/uk/me/parabola/mkgmap/osmstyle/actions/SubstringFilter.java
> new file mode 100644
> index 0000000..823d402
> --- /dev/null
> +++ b/src/uk/me/parabola/mkgmap/osmstyle/actions/SubstringFilter.java
> @@ -0,0 +1,66 @@
> +/*
> + * Copyright (C) 2010 Jeffrey C. Ollie
> + *
> + *  This program is free software; you can redistribute it and/or modify
> + *  it under the terms of the GNU General Public License version 2 as
> + *  published by the Free Software Foundation.
> + *
> + *  This program is distributed in the hope that it will be useful,
> + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
> + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + *  GNU General Public License for more details.
> + *
> + *
> + * Author: Jeffrey C. Ollie
> + * Create date: 08-March-2010
> + */
> +package uk.me.parabola.mkgmap.osmstyle.actions;
> +
> +import uk.me.parabola.mkgmap.reader.osm.Element;
> +
> +/**
> + * Extract a substring from a value
> + *
> + * @author Jeffrey C. Ollie
> + */
> +public class SubstringFilter extends ValueFilter {
> +    private int args;
> +    private int start;
> +    private int end;
> +
> +    public SubstringFilter(String arg) {
> +       start = 0;
> +       end = 0;
> +       args = 0;
> +
> +       String[] temp = arg.split(":");
> +
> +       try {
> +           if (temp.length == 1) {
> +               start = Integer.parseInt(temp[0]);
> +               args = 1;
> +           } else if (temp.length == 2) {
> +               start = Integer.parseInt(temp[0]);
> +               end = Integer.parseInt(temp[1]);
> +               args = 2;
> +           } else {
> +               start = 0;
> +               end = 0;
> +               args = 0;
> +           }
> +       } catch (NumberFormatException e) {
> +       }
> +    }
> +
> +    protected String doFilter(String value, Element el) {
> +       if (value == null) return null;
> +
> +       if (args == 1) {
> +           return value.substring(start);
> +       }
> +       if (args == 2) {
> +           return value.substring(start, end);
> +       }
> +       return value;
> +    }
> +}
> diff --git a/src/uk/me/parabola/mkgmap/osmstyle/actions/ValueBuilder.java b/src/uk/me/parabola/mkgmap/osmstyle/actions/ValueBuilder.java
> index 27b861d..1100b15 100644
> --- a/src/uk/me/parabola/mkgmap/osmstyle/actions/ValueBuilder.java
> +++ b/src/uk/me/parabola/mkgmap/osmstyle/actions/ValueBuilder.java
> @@ -171,6 +171,8 @@ public class ValueBuilder {
>                        item.addFilter(new HeightFilter(arg));
>                } else if (cmd.equals("not-equal")) {
>                        item.addFilter(new NotEqualFilter(arg));
> +               } else if (cmd.equals("substring")) {
> +                       item.addFilter(new SubstringFilter(arg));
>                }
>        }
>
> --
> 1.7.0.1
>
>



-- 
Jeff Ollie



More information about the mkgmap-dev mailing list