Jump to content

[SOLVED] Parsing GIS Geometries


flyhoney

Recommended Posts

Hello regex masters.  I am parsing GIS Geometry objects and need a bit of help.  I have a string that looks like this:

 

MULTIPOLYGON(((-91.930386 40.80865,-91.930386 40.80865[etc...])),((-91.930179 40.808662,-91.930386 40.80865[etc...])))

 

It is a list of N polygons, so if N = 1, it looks like this:

 

MULTIPOLYGON(((-91.930386 40.80865,-91.930179 40.808662[etc...])))

 

If N = 2, it looks like this:

 

MULTIPOLYGON(((-91.930386 40.80865[etc...])),((-91.930179 40.808662[etc...])))

 

Etc...

 

The current expression I am using only handles the N = 1 case:

 

/POLYGON[\(]+(.*)[\)]+/i

 

This effectively grabs all the coordinates in a string so I can explode() them.  Can you freaks help me expand this regex to account for N > 1?

 

Let me know if none of that made sense.

 

 

 

Link to comment
Share on other sites

I think I've figured it out actually, this seems to be doing what I want:

 

[\(]+(.*?)[\)]+

 

You can actually use [(]+([^)]+) -or- \(+([^)]+)

In your example, note that in a character class, you do not need to escape characters like (, as those metacharacters are striped of their special abilities and thus rendered as literals when inside a class.. In fact, there are not many meta characters that survive as such when inside a class, and this largely depends on their location..

 

By example, ^ must be the first one listed within the class to make the class a negated one, otherwise, it is treated as a literal.. the dash must be placed as either the very first or very last character to be a dash literal, otherwise, it creates a range.

Link to comment
Share on other sites

Thanks for the advice nrg_alpha.  I was unaware of that behavior.  I ended up tweaking the expression some more, but I'm away from my home computer and can't remember what I changed it to, but either way, works like a gem.

 

Thanks guys.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.