Jump to content
aa2zz6

Arrays with StringRegExp

Recommended Posts

aa2zz6

I have an Imagery Extent files with the following XMin | ymin | xMax | YMax. When I extract the information with my py script they don't come out in the format that I need in order to download Imagery: 

Python script results:

Website http://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-classes/extent.htm
python results

XMin: 484448.1122, YMin: 4474944.4993
XMax: 485156.6482, YMax: 4475870.8834

This is what I'm trying to achieve:

Correct Format to DL imagery
{"xmin":484448.1122,"ymin":4474944.4993,"xmax":485156.6482,"ymax":4475870.8834,"spatialReference":{"wkid":26917}}

Edit: This doesn't seem to be posting correctly either

I figured out how to get the first set XMin and YMin to print to results.txt but I don't know how to do the XMax and YMax on the second line :\  

The goal is to get both extents into the correct format in the ImageryExtent.txt file and copy the results to the results.txt. Also is there a reason why we can't do a quotation? When I do the file write line & " " " & it messes the script. I switch to a single & " ' " & but I haven't tested to see if that would work

Script:

#include <array.au3>

; Imagery Extent
Local $ImageryExtent = "C:\Users\aa2zz6\Desktop\PyResults\ImageryExtent.txt"

; Split for file
Local $xmin, $ymin, $xmax, $ymax

; Store results
Local $results = "C:\Users\aa2zz6\Desktop\PyResults\Results.txt"


; Read the current script file into an array using the filepath.
Local $aArray = FileReadToArray($ImageryExtent)
If @error Then
    MsgBox($MB_SYSTEMMODAL, "", "There was an error reading the file. @error: " & @error)
Else
    For $i = 0 To UBound($aArray) - 2 ; only loop as far as the penultimate element - to avoid array bounds errors
        If StringRegExp($aArray[$i], "XMin") Then
            $xmin = $aArray[$i]
            If StringRegExp($aArray[$i], "YMin") Then
                $ymin = $aArray[$i]
                FileWriteLine($results, "'xmin'" & ":" & $xmin & "," & "'ymin'" & ":" & $ymin & "," & "'xmax'" & ":" & $xmax & "," & "'ymax'" & ":" & $ymax & "," & "'spatialReference':{'wkid:26917'}}" & @CRLF)
            EndIf
        EndIf
    Next
EndIf

 

ImageryExtent.txt

Results.txt

Edited by aa2zz6

Share this post


Link to post
Share on other sites
jchd

Would this work for you?

; Imagery Extent
Local $ImageryExtent = "ImageryExtent.txt"

; Read the current file
Local $sInput = FileRead($ImageryExtent)
If @error Then
    MsgBox($MB_SYSTEMMODAL, "", "There was an error reading the file. @error: " & @error)
Else
    Local $sResult = StringRegExpReplace($sInput, "(?im)XMin: (-?\d+\.\d+), YMin: (-?\d+\.\d+)\RXMax: (-?\d+\.\d+), YMax: (-?\d+\.\d+)\R", _
        '{"xmin";$1,"ymin":$2,"xmax":$3,"ymax":$4,"spatialReference":{"wkid":26917}}')
    ConsoleWrite($sResult & @LF)    ; optional visual check
    ; write results to file
    Local $hOut = FileOpen("Results.txt", $FO_OVERWRITE)
    FileWrite($hOut, $sResult)
    FileClose($hOut)
EndIf

 

  • Like 2

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
aa2zz6
14 hours ago, jchd said:

Would this work for you?

; Imagery Extent
Local $ImageryExtent = "ImageryExtent.txt"

; Read the current file
Local $sInput = FileRead($ImageryExtent)
If @error Then
    MsgBox($MB_SYSTEMMODAL, "", "There was an error reading the file. @error: " & @error)
Else
    Local $sResult = StringRegExpReplace($sInput, "(?im)XMin: (-?\d+\.\d+), YMin: (-?\d+\.\d+)\RXMax: (-?\d+\.\d+), YMax: (-?\d+\.\d+)\R", _
        '{"xmin";$1,"ymin":$2,"xmax":$3,"ymax":$4,"spatialReference":{"wkid":26917}}')
    ConsoleWrite($sResult & @LF)    ; optional visual check
    ; write results to file
    Local $hOut = FileOpen("Results.txt", $FO_OVERWRITE)
    FileWrite($hOut, $sResult)
    FileClose($hOut)
EndIf

 

This is almost perfect but it's sticking them side-by-side. I tried adding a [& @CRLF] in the FileWrite so that it takes each result and separates each result to a new line but it doesn't seem to work as I expected. I've used @CRLF in past projects so I'm unsure as to why it wouldn't work in this instance

FileWrite($hOut, $sResult & @CRLF)

 

Share this post


Link to post
Share on other sites
iamtheky

just ran post #2 with the extent attached to the OP, and the results file is already separated when i open it in Notepad (see attached).  Can you show a small subset of what you intend the result to look like?

Results.txt

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites
jchd

@aa2zz6,

Your code adds a single CRLF right at the end of the output file, probably not what you want.

In the input file, each group of two lines is separated by a CRLF which I left in place.
In case you need to separate every output line by an empty line, just remove the final \R from the regpexp pattern string.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×