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

1029
16
04-13-2021 12:06 AM
by
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).

Tags (4)
1 Solution

Accepted Solutions
Esri Contributor

The following script should work:

``````%Expression:
re.sub(compiling, mround, !SHAPE.wkt!)

%Code Block:
import re
compiling = re.compile(r"\d*\.\d+")
def mround(wkt):
return "{:.3f}".format(float(wkt.group()))``````

16 Replies
MVP Honored Contributor
``````def regex(!WKT!):
return re.replace (!wkt!``````

you switch text case... !WKT! keep it consistent if that is the name of the field fix the re.replace section

... sort of retired...
Esri Contributor

The following script should work:

``````%Expression:
re.sub(compiling, mround, !SHAPE.wkt!)

%Code Block:
import re
compiling = re.compile(r"\d*\.\d+")
def mround(wkt):
return "{:.3f}".format(float(wkt.group()))``````

by
New Contributor II

Thanks 🙂

Got Error - Invalid Expression

I've attached screenshot and the FGDB (missed it in the first post)

Esri Contributor

You changed the code I provided, in your Code Block, that's why you're getting the error.

I also tried it on your dataset and I'm getting the right output.

Input: MULTIPOLYGON (((198102.99230000004 666117.40980000049, 198102.99230000004 656117.40980000049, 208102.99230000004 656117.40980000049, 208102.99230000004 666117.40980000049, 198102.99230000004 666117.40980000049)))

Output:  MULTIPOLYGON (((198102.992 666117.410, 198102.992 656117.410, 208102.992 656117.410, 208102.992 666117.410, 198102.992 666117.410)))

by
New Contributor II

It worked 🙂

Many thanks

MVP Honored Contributor

perhaps the following.

You have to use the mround function and pass in the SHAPE field I think

``````%Expression:
mround(!SHAPE!)

%Code Block:
import re
def mround(!SHAPE!):
compiling = re.compile(r"\d*\.\d+")
re.sub(compiling, mround, !SHAPE!.wkt)
return "{:.3f}".format(float(wkt.group()))``````

... sort of retired...
by
New Contributor II

Thanks

Got error:

File "<string>", line 2

def mround (!SHAPE!):

SyntaxError: Invalid Sytax

MVP Honored Contributor
``````%Code Block:
import re
def mround(fld):
compiling = re.compile(r"\d*\.\d+")
re.sub(compiling, mround, fld.wkt!)
return "{:.3f}".format(float(wkt.group()))``````

... sort of retired...
MVP Esteemed Contributor

For a moment @DanPatterson , I thought you were proposing a regex solution right off the bat, and then I realized you were tweaking the OP's regex.  🙂