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

1029
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
1 Solution

Accepted Solutions
MehdiPira1
Esri Contributor

@yovavzo ,

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()))

 

 

 

View solution in original post

0 Kudos
16 Replies
DanPatterson
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...
0 Kudos
MehdiPira1
Esri Contributor

@yovavzo ,

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()))

 

 

 

View solution in original post

0 Kudos
yovavzo
New Contributor II

Thanks 🙂

Got Error - Invalid Expression

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

0 Kudos
MehdiPira1
Esri Contributor

@yovavzo ,

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

MehdiPira1_0-1618362174888.png

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

MehdiPira1_0-1618385289808.png

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)))

 

0 Kudos
yovavzo
New Contributor II

My bad

It worked 🙂

Many thanks

0 Kudos
DanPatterson
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...
0 Kudos
yovavzo
New Contributor II

Thanks

Got error:

File "<string>", line 2

def mround (!SHAPE!):

SyntaxError: Invalid Sytax

 

0 Kudos
DanPatterson
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...
JoshuaBixby
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.  🙂

0 Kudos