MapInfo Pro

 View Only

MapInfo Monday: SQL Select Exercises in MapInfo Pro

  • 1.  MapInfo Monday: SQL Select Exercises in MapInfo Pro

    Employee
    Posted 01-13-2025 06:52
    Edited by Peter Møller 29 days ago

    Moving forward, some of the #MapInfoMonday posts will allow you to practice your MapInfo Pro skills. Using MapInfo Pro and sample data from the Precisely Data Experience, I will give you a few exercises and solutions. As always with MapInfo Pro, you can solve a problem in many ways.

    I would also like to challenge you to provide alternative solutions to the exercises so that we all get wiser. Thanks!

    This is the first of such an article.

    Prerequisites

    From the Precisely Data Experience, create an account and log in.

    Now click on the Sample Datasets button in the Explore section.
    Click on the Street tab and download one of the StreetPro (Display) datasets. I will be using the sample for Germany.
    Once downloaded, unzip the file so that you can access the data from MapInfo Pro.
    In MapInfo Pro open one of the workspaces. I'll be using the workspace deu_streetpro.wor.
    In MapInfo Pro open the SQL Window from the SQL dropdown on the Table, Map, or Spatial tabs.

    Exercise 1

    From the table DEU_Business_Locations select electric vehicle stations using the column Category. Query the records into a query named qEV_Stations.
    Add the query to your map and style the layer with an appropriate symbol.

    Exercise 2

    Change the query to only include two columns: Name and Category.

    Exercise 3

    Create a 500-meter around all highways. The table DEU_Street_1 holds all the highways.

    Name the result qHighwaysBuffer, add the result to your map, and style it using a dotted red line with a translucent white fill.

    Exercise 4

    Find all the electric vehicle stations that are within a 500 distance of the highways, the roads in the DEU_Street_1 table.

    Solution 1

    In the SQL Window, select the table DEU_Business_Locations from the Table list. Make sure it's inserted after the From keyword.
    Insert the Where keyword a new line before the Into keyword.
    From the Column list, select the Category column. Make sure it is inserted after the Where keyword with a space in between.
    Now type in the = operator or select it from the Operator list.
    Finally, you can select the value you are looking for from the Value list. Try to filter the list by entering a few letters from the word.
    Change the word Selection to qEV_Stations in the last line of the Select statement.
    The final query looks like this:
    Select *
    From DEU_Business_Locations
    Where DEU_Business_Locations.Category = "Electric Vehicle Station"
    Into qEV_Stations

    Before executing the statement, make sure to uncheck the Display in Browser and check the Add to Map option at the bottom of the SQL Window.

    With the resulting query opened in the map, click on the thumbnail next to the layer to open the Symbol Style dialog where you can set a style override for the entire layer.

    The resulting map looks like this.

    Solution 2

    Delete the * after the Select keyword and keep the cursor positioned here. The * tells the Select statement to bring back all columns from the table(s).

    Now from the Column list, click on the Name column and then on the Category column to insert these at the cursor position. You can also double-click on the last column you want to select from the list. This will close the list.

    Select DEU_Business_Locations.Name, DEU_Business_Locations.Category
    From DEU_Business_Locations
    Where DEU_Business_Locations.Category = "Electric Vehicle Station"
    Into qEV_Stations

    When you execute the query, the previous query will be closed and the new will be added to the map window. You will have to change the Style Override again.

    Use the Info tool to query one of the points in the query. Notice that the Info window only shows the two columns included in the Select statement. Note that the spatial object also is included as the query can be added to the map window.

    Solution 3

    In the SQL Window, click on the Create New Script button to create a new Select script.

    Position your cursor after the From keyword and select the table DEU_Street_1 from the Table list.

    Delete the * after the Select keyword. Enter the text HighwayBuffer in quotes. This will be an attribute assigned to the resulting buffer object. Add As "Type" to rename the resulting column to Type.

    Now from the Aggregate list, select AggregateBuffer which will combine all the records and add a buffer around the combined object. Let's specify a resolution of 36 which is the first parameter after obj, and set a buffer width of 500 meters. 500 should be the third parameter and "m" the fourth and last parameter.

    Change the output query name from Selection to qHighwayBuffer.

    The final query looks like this:

    Select "HighwayBuffer" As "Type", AggregateBuffer(obj, 36, 500, "m")
    From DEU_Street_1 
    Into qHighwayBuffer

    Before running the query, again make sure the resulting query table gets added to your map and not to a browser.

    Drag the query to the top of the map, and click on the style thumbnail to pen the Region Style dialog. Change the styling.

    Finally, with the layer selected go to the Style tab and set the Layer Translucency to 50%.
    You should now see a map like this.

    Solution 4

    We will be using the query tables that we have been creating in the exercises until now.

    In the SQL Window, click on the Create New Script button to create a new Select script.

    From the Table list, select the query qEV_Stations.

    Insert a new line before the Into keyword and add the Where keyword.

    Now type in OBJ Within (Select. You may have to hit the Esc key here to stop IntelliSense from replacing Select with a suggestion.

    Containing typing OBJ From qH. You can now use IntelliSense to insert the name of the query holding the highway buffer. Click on the Ctrl + Space keys to open the IntelliSense suggestion list. Select the correct table name from the list and hit the Enter key to insert it. Add the final ).

    This is the final SubSelect query that will select all the EV Stations inside the highway buffer.

    Select *
    From qEV_Stations
    Where OBJ Within (Select OBJ From qHighwayBuffer)
    Into qEV_Stations_Near_Highways

    Before executing the query, make sure the query result is added to your map and not to a browser window.

    Change the Layer Style Override to show a bigger icon similar to the icon used for the qEV_Stations layer.

    I'm looking forward to your great alternative solutions.



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