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
https://forums.phpfreaks.com/topic/141390-solved-parsing-gis-geometries/
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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.