Hi George
The Python implementation in MapInfo Pro builds on top of our .NET APIs. Unfortunately, these APIs do not cover the full breadth of MapBasic yet. This means that you'll have to use a combination of Python and MapBasic to do what you want to.
Here's a very simple example that uses Python to loop over a number of predefined columns, create a thematic for each of these and save the resulting map to an image:
import os
print("Starting now")
sTab = 'time_series_covid19_deaths_US'
themeColumns = [
_3_9_20',
'_3_8_20',
'_3_7_20',
'_3_6_20',
'_3_5_20',
'_3_4_20',
'_3_3_20',
'_3_2_20',
'_3_1_20'
]
for col in themeColumns:
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(col)
print(fileName)
do('Save Window FrontWindow() As "{}" Type "PNG" Resolution 300'.format(fileName))
do('Remove Map Window FrontWindow() Layer 1')
print('Finished')
We have adjusted the standard
print()
method in Python to print to the Message window.
The
do
method lets you pass a MapBasic command to MapInfo Pro and have it executed. Similarly, you can use the
Eval()
method to pass a Mapbasic function and get the response from this function.
In my example, I'm using Python to handle the looping but passing the MapBasic statements to MapInfo Pro using the
do
method.
So the answer to your two first questions is to use the do method. You can also use the
OpenTable
method on the
Catalog
object to open a table into the current MapInfo Pro session.
Python has it's own error handling that you of course can use when writing Python add-ins for MapInfo Pro. Here's a very basic example:
try:
print(x)
except:
print("An exception occurred")
------------------------------
Peter Horsbøll Møller
Distinguished Engineer
Pitney Bowes Software & Data
------------------------------
Original Message:
Sent: 06-15-2020 00:36
From: George Corea
Subject: Using Python can we assign symbology to a dataset?
In MI 2019.2 I am looking at using some of the example scripts to create menu items and add multiple groups of tables with preset symbology, themes, legends and hotlinks.
1. 'Ribbon.py', 'choose table.py' and 'open tool windows.py' makes it simple to add a ribbon button and have the open table dialog but I don't see the python code to directly add a .tab file.
2. How do I assign custom symbology, themes, legends and hotlinks to it in python?
- I noticed 'MapInfoTableInfo.py' and 'LayerProperties.py' allows users to see the assigned symbology (layer style) in a tab but is there a way to assign this in python?
I know it will bring in the default themes/hotlinks from the metadata of the .tab file but Ideally rather than having to code each color etc would it be possible to get the theme to use from another file where the theme and hotlinks can be saved. This is when we need to have different symbology than the default. In QGIS it's a QLR/QML file that can be loaded to overwrite the default symbology in python quite simply
In Mapbasic it would be
Open Table "G:\GIS_Tables\S_Mains_ExclAbandoned" As S_Mains_ExclAbandoned Interactive
Add Map Auto Layer S_Mains_ExclAbandoned
shade S_Mains_ExclAbandoned with Type values
"Gravity Main" Line (25,2,16711680) ,
"Recycled Water" Line (3,3,10502399) ,
"Rising Main" Line (4,3,16711680)
Set Map
Layer prev
Display Value
Global Pen (1,2,0) Brush (2,16777215,16777215) Symbol (35,0,12) Line (1,2,0) Font ("Arial",0,9,0)
Selectable Off
Zoom (0, 12) Units "km"
Layer S_Mains_ExclAbandoned
Display Graphic
Global Pen (1,2,0) Brush (1,16777215,16777215) Symbol (35,16776960,12,"MapInfo Symbols",16,0) Line (1,2,0) Font ("Arial",0,9,0)
Activate
Using "https://edrms/docs/~D"+As_con_InfoXpert_ID Alias "InfoXpert primary link: " On Labels Objects Relative Path Off,
Using "https://edrms/docs/~D"+CWCF_hyperlink Alias "Additional Details docs: " On Labels Objects Relative Path Off,
Using "http://council/tickets/v2#open_tickets/"+Spiceworks_ID Alias "Spiceworks: "+Spiceworks_ID On Labels Objects Relative Path Off
Label Line Arrow Position Above Auto Retry On Font ("Arial",261,7,16711680) Pen (1,2,0)
With Diameter+"Ø "+Material+" "+Install_Year
Parallel On Auto On Overlap Off Duplicates On Offset 2
Visibility Zoom (0, 1) Units "km"
set legend
layer Prev
display on
shades off
symbols off
lines on
count off
title "Sewer Mains" Font ("Arial",1,8,0)
subtitle auto Font ("Arial",1,8,0)
'ascending on
ranges Font ("Arial",0,8,0)
auto display off,
auto display on
3. Is there a routine for error capturing?
Thanks for your advice.
------------------------------
George Corea
Mangoesmapping
------------------------------