Select to view content in your preferred language

field calc (pro 2.7) - reduce WKT length by Rounding/Truncating decimal places

4083
17
Jump to solution
04-13-2021 12:06 AM
yovavzo
Emerging Contributor

Hi,

I need to reduce WKT (Well Known Text) length.

The reduction is done by rounding/trunkating decimal places from 11 to 3 - default output for the script !shape.wkt! returns coordinate list with 11 decimal places (in a metric projected coordinate system).

I ran across a RegEx expression that might do the job, but it returns an error (arcGIS Pro 2.7 - python):

%Expression:
regex(!WKT!)
%Code Block:
import re
def regex(!WKT!):
  return re.replace (!wkt!, '(\d+. \d{4}) \d+)', '\1')

 I'm not experianced python user.

I'd be happy to get help with solution - it doesnt has to be this expression - any creativ/simple solution (in Field Calculator) is welcomed.

I attached a sample layer (FGDB).

Thanks in advance,

0 Kudos
17 Replies
DanPatterson
MVP Esteemed Contributor

tweaking, not my goto for sure 😉

 


... sort of retired...
0 Kudos
yovavzo
Emerging Contributor

Thanks again 🙂

Got another error (when checkd validation):

File "<string>", line 4

  re.sub(compiling, mround, fld.wkt!)

SyntaxError: Invalid Sytax

0 Kudos
DanPatterson
MVP Esteemed Contributor

you have a single !  if that is the problem.   !fld.wkt!  which look strange in the first place thought it would be !SHAPE@WKT! as is used in cursors

So my version didn't work? referencing the shape field?


... sort of retired...
0 Kudos
yovavzo
Emerging Contributor

Both cases didn"t work (attached screenshots) 

0 Kudos
yovavzo
Emerging Contributor

You'r right

replied to your original reply - thanks

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

To follow-up or add on to @MehdiPira1 , you can make the precision a parameter of the function:

# expression
wkt_precision(!SHAPE.wkt!, 3)

# code block
import re
def wkt_precision(wkt, precision)
    return re.sub("(\d+\.\d+)", lambda m: f"{float(m.group()):.{precision}f}", wkt)
GIS_Spellblade
Frequent Contributor

Just a minor follow-up on this; depending on the complexity of your feature (especially dense, un-simplified multipolygons), reducing the precision of the WKT could cause some vertices to become coincident, break topology, or otherwise create invalid geometries that will make your table un-selectable within the ArcGIS Pro GUI.

I was dealing with some coastal polygons and this issue tripped me up and I ended up needing to move up to a precision 7 to negate the issue. Also, relevant XKCD: 

0 Kudos