MapInfo Pro

 View Only
  • 1.  MapInfo Monday: Creating multiple images using Python

    Employee
    Posted 09-14-2020 04:39
      |   view attached
    This week's tip and trick is about using Python to generate a series of images from a map. It's also a bit more technical than the earlier #MapInfoMonday Tips & Tricks.

    There are multiple scenarios where you want to create multiple versions of the "same" map. You might have multiple locations, you want to be printed, maybe you want to see the same location but slowly zooming into the location from a larger area, and then there is this scenario where you want to show values for one location as they change over time.

    I have downloaded the Covid-19 data from Johns Hopkins University via the Github repository they have setup. This gives you access to a number of CSV files with data from all over the World as current status or as time series where you get all the numbers since early March.

    In my case, I'm using data from early March into Mid-April. You can of course extend this to go all the way up to the most recent dataset. I'm focusing on the United States but you could also have chosen the entire World as that data also is available.

    I have opened the CSV file into MapInfo Pro, created points using the Lat/Long values in the data, added the data to a map where I also have the Precisely Iron map, and  I have made the Covid-19 data layer invisible. My script will add a thematic layer to the map using data for each of the days which is why I don't need to display the actual data points from the beginning.

    Below you can see how my map looks, and you can also see the Python script in the Python console window at the bottom.

    Let me take you through the script in some smaller steps.

    I'm using the print statement to write a bit of text to the Message window and specifying the table to be used in the loop later on.
    print("Starting now")
    sTab = 'time_series_covid19_deaths_US'

    Next, I need to specify the columns to loop through and a more friendly name to be used for the files generated. For this, I'm creating a list containing multiple lists. The inner lists hold two elements; the column name and the friendly name.
    themeColumns = [
         ['_4_14_20', '2020-04-14'], ['_4_13_20', '2020-04-13'], ['_4_12_20', '2020-04-12']
       , ['_4_11_20', '2020-04-11'], ['_4_10_20', '2020-04-10'], ['_4_9_20', '2020-04-09']
       , ['_4_8_20', '2020-04-08'], ['_4_7_20', '2020-04-07'], ['_4_6_20', '2020-04-06']
       , ['_4_5_20', '2020-04-05'], ['_4_4_20', '2020-04-04'], ['_4_3_20', '2020-04-03']
       , ['_4_2_20', '2020-04-02'], ['_4_1_20', '2020-04-01'], ['_3_31_20', '2020-03-31']
       , ['_3_30_20', '2020-03-30'], ['_3_29_20', '2020-03-29'], ['_3_28_20', '2020-03-28']
       , ['_3_27_20', '2020-03-27'], ['_3_26_20', '2020-03-26'], ['_3_25_20', '2020-03-25']
       , ['_3_24_20', '2020-03-24'], ['_3_23_20', '2020-03-23'], ['_3_22_20', '2020-03-22']
       , ['_3_21_20', '2020-03-21'], ['_3_20_20', '2020-03-20'], ['_3_19_20', '2020-03-19']
       , ['_3_18_20', '2020-03-18'], ['_3_17_20', '2020-03-17'], ['_3_16_20', '2020-03-16']
       , ['_3_15_20', '2020-03-15'], ['_3_14_20', '2020-03-14'], ['_3_13_20', '2020-03-13']
       , ['_3_12_20', '2020-03-12'], ['_3_11_20', '2020-03-11'], ['_3_10_20', '2020-03-10']
       , ['_3_9_20', '2020-03-09'], ['_3_8_20', '2020-03-08'], ['_3_7_20', '2020-03-07']
       , ['_3_6_20', '2020-03-06'], ['_3_5_20', '2020-03-05'], ['_3_4_20', '2020-03-04']
       , ['_3_3_20', '2020-03-03'], ['_3_2_20', '2020-03-02'], ['_3_1_20','2020-03-01']
    ]

    Now it's time to do the actual loop where I run through all the lists in the list themeColumns. I get the two values from the individual elements of the list, use the do method to pass a Shade statement to MapInfo Pro, create a friendly file name using the nicely formatted date, and then I use another two do statements; one to export the window into a PNG file and the other to remove the thematic layer from the map window.

    for theme in themeColumns:
       col = theme[0]
       date = theme[1]
       do('shade window FrontWindow() 1 with {} ignore 0 graduated 0.0:0 10000:24 Symbol (35,12583104,36,"MapInfo Symbols",0,0) Symbol (35,255,36,"MapInfo Symbols",0,0) vary size by "LOG"'.format(col))
       fileName = r'D:\3. Demo\2. Maps\2020\Covid19\04-14 Data April 14\Time Series\US {}.PNG'.format(date)
       do('Save Window FrontWindow() As "{}" Type "PNG" Resolution 300'.format(fileName))

       do('Remove Map Window FrontWindow() Layer 1')

    And finally, I use another print statement to write the word Finished to the Message window.
    print('Finished')

    I have attached the full source code as a text file below.

    A single exported image looks like this

    And if you create a GIF from all the files, it can look like this

    You can do a lot using a few lines of Python code. Python is one of the most used development languages and we are glad to also be able to give MapInfo Pro users access to using Python as an alternative to and in combination with MapBasic. When you are using the Python Console instead of the MapBasic Window, you get at least two major benefits besides the Python language itself: You can use loops, as I just showed above, and you can also use If statements. These two alone make it way more powerful compared to the MapBasic Window.

    I hope you found this tip useful. If you have any questions, or ideas for next week's #MapInfoMonday tip and trick, use the Comments section below.

    #MapInfoMonday is your weekly little tip​ to help you get more from your MapInfo Pro.

    ------------------------------
    Peter Horsbøll Møller
    Principal Presales Consultant | Distinguished Engineer
    Precisely | Trust in Data
    ------------------------------

    Attachment(s)



  • 2.  RE: MapInfo Monday: Creating multiple images using Python

    Employee
    Posted 09-14-2020 10:01
    PS: I'd better add that with the upcoming MapInfo Pro v2019.3, we will be supporting a more direct way to work with maps, layers, and thematic from Python and .NET.

    This means that you don't have to use the do method to pass a MapBasic statement to MapInfo Pro to create a thematic map.

    More on this later

    ------------------------------
    Peter Horsbøll Møller
    Principal Presales Consultant | Distinguished Engineer
    Precisely | Trust in Data
    ------------------------------



  • 3.  RE: MapInfo Monday: Creating multiple images using Python

    Posted 09-15-2020 08:20
    Thanks for the article Peter, great tips!

    Looking forward to see the Python improvements in MapInfo v.2019.3!

    Julien

    ------------------------------
    Julien Lebrun
    Korem Geospatial Software & Data
    Quebec QC
    ------------------------------



  • 4.  RE: MapInfo Monday: Creating multiple images using Python

    Posted 09-15-2020 10:40
    Interesting, thanks. alhough, surely Mapbasic does have several loops, and Iif, so is there really an advantage?

    ------------------------------
    Martin Burroughs
    Oldham Council
    ------------------------------



  • 5.  RE: MapInfo Monday: Creating multiple images using Python

    Employee
    Posted 09-15-2020 12:21
    Correct, Martin. MapBasic has several ways to loop and handle conditions, but none of these can be used in the MapBasic Window for writing small scripts.

    You can only use these through a compiled MapBasic application.

    You can however use the IIf() and Cond() functions in MapBasic scripts.

    ------------------------------
    Peter Horsbøll Møller
    Principal Presales Consultant | Distinguished Engineer
    Precisely | Trust in Data
    ------------------------------



  • 6.  RE: MapInfo Monday: Creating multiple images using Python

    Employee
    Posted 09-15-2020 16:07
    Very nice Peter.

    I am sure there we could find a Python module to create the gif as well.

    -Bob

    ------------------------------
    Bob Fortin
    Software Architect and Distinguished Engineer
    MapInfo Pro Development Team
    ------------------------------



  • 7.  RE: MapInfo Monday: Creating multiple images using Python

    Employee
    Posted 09-16-2020 04:00
    Or add support for Windows to the .NET and Python object model?

    winMap.Export(someFile, "PNG", 200, 5, 10, "cm")

    But then again using the MapBasic statement via the Do method is quite easy through Python as you can use '' as the outer quotes and "" internally in the string.

    ------------------------------
    Peter Horsbøll Møller
    Principal Presales Consultant | Distinguished Engineer
    Precisely | Trust in Data
    ------------------------------



  • 8.  RE: MapInfo Monday: Creating multiple images using Python

    Employee
    Posted 09-16-2020 08:15
    I meant the animated gif part

    ------------------------------
    Bob Fortin
    Software Architect and Distinguished Engineer
    MapInfo Pro Development Team
    ------------------------------



  • 9.  RE: MapInfo Monday: Creating multiple images using Python

    Employee
    Posted 09-16-2020 08:17
    Right, of course.
    Yeah, that could be nice.

    ------------------------------
    Peter Horsbøll Møller
    Principal Presales Consultant | Distinguished Engineer
    Precisely | Trust in Data
    ------------------------------