TNOCS / minify-geojson

Minifies (compresses) a GeoJSON file, i.e. by removing properties or using fewer decimals for coordinates.
MIT License
26 stars 3 forks source link

Filters not properly parsing RHS values #27

Closed achmurzy closed 4 years ago

achmurzy commented 4 years ago

I am trying to filter two features: "'MARINE = 0, REP_AREA > 100". The first feature is parsed properly by the regex given on line 52 of src/utils.js

const re = /^([a-zA-Z 0-9]*) ?([!<>=]{1,2}) ?([a-zA-Z -0-9]*)$/;

but the second is not. If I change the first feature to "MARINE = 6", it too fails. I think there is an issue with the RHS VALUE term in the regex [...-0-9], perhaps not handling the negative sign interacting with the digits term properly? I'm not sure how to write the regex properly but if I remove the negative sign it works properly for me (since my features aren't using negative values).

achmurzy commented 4 years ago

After looking in to it and some testing, it looks like for negative number an escape character is needed to say the negative sign: '-' so the regex definition should be

const re = /^([a-zA-Z_ 0-9]*) ?([!<>=]{1,2}) ?([a-zA-Z_ \-0-9]*)$/;

I only briefly tested this for numeric features but it permits both positive and negative values

erikvullings commented 4 years ago

@achmurzy Thanks for catching this bug. And indeed, you are right that the current regex is wrong. It erroneously tries to create a range of characters between a space and 0, so escaping the \- does solve the problem. I've applied your fix (and updated the dependencies), so now we have:

const re = /^([a-zA-Z_\-0-9]*) *([!<>=]{1,2}) *([a-zA-Z_\-0-9]*)$/;

I'll release a new patch, so please give it a go.