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

2768
16
Jump to solution
04-13-2021 12:06 AM
yovavzo
New Contributor II

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
16 Replies
DanPatterson
MVP Esteemed Contributor

tweaking, not my goto for sure 😉

 


... sort of retired...
0 Kudos
yovavzo
New Contributor II

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
New Contributor II

Both cases didn"t work (attached screenshots) 

0 Kudos
yovavzo
New Contributor II

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)