C # Develop GIS Application Concise Tutorial (2)

xiaoxiao2021-03-05  28

Chapter 2

ToolConstants

In the previous chapter, we tried to make a map to enlarge the map. In this chapter, we will come into contact with more tools and introduce custom tools.

Mapx provides a series of tools for developers. These tools have different effects. We understand the role of these tools by developing an app. For details of these tools, you can find "Available" in Mapx's help documentation. "STANDARD TOOLS" See the corresponding explanation. Some of the relevant information of some MapX also introduces.

In the .NET programming environment, create a "Project / Add Reference" in the menu, open the window as shown below, select the COM tab in the window, double click on the MapInfo in the component name list MapX V5. Click the "Confirm" button to add the Map5 control into the .NET toolbox.

Next, draw the MapInfo Mapx V5 control onto the form, and draw a ComboBox control comBOBOX1 on the form. As shown below:

Double-click the design form, write the form1_load code as follows:

private void Form1_Load (object sender, System.EventArgs e) {ArrayList ToolsList = new ArrayList (); ToolsList.Add (MapXLib.ToolConstants.miArrowTool); ToolsList.Add (MapXLib.ToolConstants.miCenterTool); ToolsList.Add (MapXLib.ToolConstants .miLabelTool); ToolsList.Add (MapXLib.ToolConstants.miPanTool); ToolsList.Add (MapXLib.ToolConstants.miPolygonSelectTool); ToolsList.Add (MapXLib.ToolConstants.miRadiusSelectTool); ToolsList.Add (MapXLib.ToolConstants.miSymbolTool); ToolsList. Add (MapXLib.ToolConstants.miTextTool); ToolsList.Add (MapXLib.ToolConstants.miZoomInTool); ToolsList.Add (MapXLib.ToolConstants.miZoomOutTool); comboBox1.DataSource = ToolsList;}

The above code passes a array ToolsList, adding Mapxlib's tools to ComboBoX1. Mapx has some other tools, their role is to add points, lines, polygonal and circles to the map. Because these tools require operational layer I introduce when introducing the layer. For the use of ArrayList, please refer to the relevant information of C #.

Then, double-click ComboBox1, and write code as follows.

Private void ComboBox1_selected IndexChanged (Object Sender, System.EventArgs E) {AXMAP1.CURRENTTOOL = (Mapxlib.ToolConstants) ComboBox1.SelectedItem;}

It should be noted that in the C # must be explicitly converted to MapxLib.ToolConstants, because ToolConstants is a enumeration data type defined by Mapx, C # cannot be automatically completed this enumeration Member variables to Object data conversion.

Compilation executive, select different tools from ComboBox1, do it on the map, you can learn the powerful features of the MAPX tool set.

In fact, the toolset provided by Mapx is like Photoshop, AutoCAD toolbar, providing you with some tools for controlling maps. However, when developing GIS, these tools do not fully meet the requirements. So, you should also learn Custom tool. Below, through an example of ranging on the map, introduce how to customize the MAPX tool at C #

Create a C # Windows application as previously described and add the MapInfo Mapx V5 control to the form. Add a Button control button1. Change the version of the Text property of Button1 to "ranging", then put two in the window. Label control Label1, Label2, sets their Text attributes to "" empty string, as shown below:

Double-click the design form, write the form1_load code as follows:

private void Form1_Load (object sender, System.EventArgs e) {axMap1.CreateCustomTool (100, MapXLib.ToolTypeConstants.miToolTypePoly, MapXLib.CursorConstants.miCrossCursor, null, null, null);}

When loading the window, we define a tool. Its number is 100 (don't repeat the tool number of the MapX itself), its type is a polycycle, using a cross-cursor.

For this function, it is recommended to view the development manual of MapX and related information.

Now we define a tool number 100, you can use it in the program. Double-click button1 in the design form, write its Click event handling code as follows:

Private void button1_click (object sender, system.eventargs e) {axmap1.currenttool = (MapxLib.ToolConstants) 100;}

Now compilation, click Button1, you can use this tool on the map. But there is also a part of the important code is not completed: Range!

The code that completes the ranging function is very representative in C # Developing GIS using MAPX. It is more difficult. When I first use C # MAPX to develop GIS, I have been plagued by this problem for a long time. The project manager also studied, Two days said that there was a result, but he gave an example at all. Of course, it will not be difficult. In fact, it is also very simple.

First, declare two private global variables in the window class to save the measured distance and total distance, pay attention to the location of the code:

public class Form1: System.Windows.Forms.Form {private AxMapXLib.AxMap axMap1; private System.Windows.Forms.Button button1; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; // /

/// The required designer variable. /// private system.componentmodel.container component = null; private double dis = 0, DISSUM = 0; ......

Now to write tools. Note that when Mapx is multi-line, it triggers the message and the general tools are different. First, it is not completed with a mouse, but also continue to draw. Therefore, it should not be written in the toolused event of the MAPX control, and the ranging should be implemented in the PolyToolused event. The code is as follows:

private void axMap1_PolyToolUsed (object sender, AxMapXLib.CMapXEvents_PolyToolUsedEvent e) {MapXLib.PointsClass pts = new MapXLib.PointsClass (); switch (e.flags) {case (int) MapXLib.ToolFlagConstants.miToolInProgress: pts = (MapXLib.PointsClass) e .points; dis = axmap1.distance (pts._item (pts.count-1) .x, pts._item (pts.count-1) .y, pts._item (pts.count) .x, PTS._Item PTS.Count) .y; DISSUM = Dis; Break; default: DIS = 0; DISSUM = 0; Break;} label1.text = "Distance:" dishabiztring ("#. 00"); label2.text = "Total distance" Dissum.toString ("#. 00");} This code is short. But there are many places to pay attention to.

First, define a MAPXLIB.POINTSCLASS type variable PTS, pay attention to PointSclass, not PointClass. The former is a point set, the latter is a point. Why add a variable? Because Mapx's PolyToolused event returns the member Points of the parameter e Not a mapxlib.pointsclass type, but Object. Type. So you need such a variable to convert it, of course, you can also use (MapxLib.PointSclass) E.Points in the program using PTS, but then the program is more I understand;

Second, to determine the value of the incident E.FLAGS, it points out the current state of the tool, is it just starting to draw a polynonym, or is a multi-ride line, or it is over, or the end exit. We just draw Time to raid. Time to set the distance and the total distance;

Then, pay attention to the point data saving mode of E.Points, E.Points first is an Object, when it is a multi-righteous line, it is initially tried into a variable of MapxLib.PointSclass, and in two-dimensional array Save Point Set. This array starts from 1, not from 0. It saves the coordinates of each turning point on the multispen line, with a mouse, add a new data to point set, we Calculate the last straight line length, you should take forward from the end of this array. Calculate the distance to the total distance after calculation. Many ranging program examples must do a loop, actually unnecessary.

Finally, please note the format issues of numbers to strings. In this example we keep two decimals.

In addition, because the geographic coordinate system of the map is not set, the measured distance unit is mile, if you want to change to km, change the MapUnit property of the MapX control to miunitkilometer. To increase the measurement accuracy, in addition to the conversion format When you increase the decimal position, you should also pay attention to the map of mapping accuracy. Otherwise, there is no significant difference.

Exercise

1. Establish an app, join the MAPX control, and a ComboBox control, two: Label controls, join all the tools and ranging tools in the first example in ComboBox, and make ranging functions when you select the ranging tool in ComboBox. .

2. Change the cursor of the ranging tool to an arrow cursor. And display the ranging data in meters.

2. Layers and Features (Layers and Features)

For the concepts of layers and primitives, please refer to the relevant information.

The application of the layer is divided into several aspects, we will introduce them separately.

1) Creation of the layer:

Create a permanent layer:

In C #, use the following methods to create a permanent layer:

Mapxlib.layer lyr; lyr = axmap1.layers.createlayer ("Mylayer", "D: //maptest/Mylayer.tab", 0, 32, AXMAP1.DISPLAYCOORDSYS; when this two sentences are executed, at the specified The path generates a series of files. They are:

Mylayer.dat: Data file of the layer, which saves the database data of the layer;

Mylayer.id: Unique, automatically generated encoding of layer data, is used to distinguish between different primitives;

Mylayer.ind: Index file for layer data to realize fast look at the primitive on the layer;

MYLAER.MAP: graphical data on the chamber;

MyLaer.Tab: This is a text file that is to save the information about the layer for GST map files or other programs to call the layer.

Use Notepad to open myLayer.tab file, see the following:

! Table! Version 450! Charset windowssimpchinese definition table description "Mylayer" Type Native Charset "WindowssimpCHINESE" Fields 1 Geoname Char (32) Index 1;

The first row is always "! Table", indicating that this is a layer of chart;

The second line indicates the version number of the layer file, and the layer version number generated by the MapInfo Mapx 5.0 is 450;

Third line points out the operating system generated by the layer;

Next, it is a definition section of the layer:

First, it is pointed out that the description of the layer is "Mylayer" in our program code.

Next, the character set type is Simplified Chinese;

Then point out in the form, the name of this field is "Geoname", which is a character type field with a length of 32, and the column index in the table is 1.

2) Add an existing layer:

The method of adding an existing layer and the general programming language has no great difference. We add the permanent layer created on the current layer, and the program code is as follows:

MapXLib.LayerInfo li; li = new MapXLib.LayerInfoClass (); li.Type = MapXLib.LayerInfoTypeConstants.miLayerInfoTypeTab; li.AddParameter ( "FileSpec", "D: //MapTest//MyLayer.tab"); li.AddParameter ( "Visible", False; li.addparameter ("AutoCreatedataSet", true); li.addparameter ("DatasetName", "Mylayer"); Axmap1.Layers.Add (li, 0); Axmap1.Layers.Layersdlg ("" " , ""); Axmap1.savemapasgeoset ("Test", "D: //maptest/Mymap.gst");

At the end of the program, we show the Layer Information dialog to observe if the layer has been added to the current map. It can be seen that the map has been added to the layer "Mylayer". Place the Layer 0.

Then, save the map in a MYMAP.GST map file, the title of this map is "test".

3) Remove the layer:

Ok, then the work we did, set the Geoseet property of the project's AXMAP1 to the map file just generated: "D: /maptest/mymap.gst". Now we remove myLayer from the map.

Add a button, write the following code in the click Click event:

AXMAP1.LAYERS.LAYERSDLG ("," "); axmap1.layers.remove (1); axmap1.layers.Layersdlg (", "");

We use two ways to display the layer dialog box to view the effect of the program. It should be noted that the Remove (1) is used when the REMOVE Layer 0 is used. If you don't know the location of the layer, you will make a loop. The location of the layer is removed and removed, as follows: int = 0; axmap1.layers.layersdlg ("," "); for (int i = 1; i

Removing operation is only in memory, that is, the program does not delete any files, and does not remove the layer from the map collection. When the program is restarted, the MyLayer layer is still in the map.

3) Remove all layers:

Use axmap1.layers.removeall (); you can remove all layers, usage and remove.

4) Layer positioning:

Like other programming languages, the location of the layer can be changed by using the AXMAP1.MOVE (1, 2) function.

5) Create a temporary layer

The temporary layer and the permanent layer are different, which is only stored in memory, and the layer will not exist when the program is turned off.

Here we will use the LayerInfo object to create a temporary layer, which is similar to the traditional MAPX program, but introduces some features of C # programming:

MapXLib.LayerInfoClass li = new MapXLib.LayerInfoClass (); MapXLib.Features ftrs = null; MapXLib.FieldsClass flds = new MapXLib.FieldsClass (); MapXLib.Fields Myflds = null; MapXLib.Dataset dts = null; flds.Add ( " State "," State_Name ", MapXLib.AggregationFunctionConstants.miAggregationSum, MapXLib.FieldTypeConstants.miTypeString); dts = axMap1.DataSets.Add (MapXLib.DatasetTypeConstants.miDataSetLayer, axMap1.Layers._Item (1)," MyLayer ", 0,0 , 0, flds, false; myflds = dts.fields; fTRS = AXMAP1.LAYERS._ITEM ("USA"). Selection.clone (); li.Type = Mapxlib.LayerInfotypetemp; li.addparameter ("name" "USA Temp Layer"; li.addparameter ("Fields", Myflds); li.addparameter ("features", ftrs); axmap1.layers.Add (li, 1); axmap1.layers.layersdlg ("", "" "

This program has two key places:

One is the usage of Datasets.Add in C #, in many programming languages, you can use empty parameters or simply do not use parameters to call this function, but in C #, it is not possible, and 8 parameters must be specified. Moreover, The Fields parameters should be initialized in advance. This function is involved in many knowledge, including understanding of MAPX related concepts and C # programming, is a very important, more difficult to master technology. In the following chapters, we will also make Further discussion.

The other is li.addparameter and li.Type, which is discussed in many MAPX books. This is no longer repeated. The above programs are best to figure out how much you go. C # development gis The essentials.

6) Zoom layer:

The so-called zoom layer does not refer to the scalament of a single layer. Instead specify the visible range ratio of the layer, for example, set a layer to hide when the display is greater than 5 miles. When it is less than 5 miles, it is displayed. And other Like the programming language, just set the zoommin and zoommax of Layer.

7) Show out the entire layer:

One of the skills to mention here is how to display the entire layer under C #. As well as the following:

Map1.bounds = map1.layers ("USA"). Bounds

A program can be easily implemented. But if you write this in C #, 100% will be wrong. In fact, there is a small skill here, and you can see it in smart:

Axmap1.cts = axmap1.layers._item ("USA"). Bounds;

8) Draw a permanent graphic on the layer:

When we introduce tools, there are some tools that have not been introduced, which is actually used to create a permanent graphic object on the layer (primitives). These graphics will be recorded after the graph is drawn on the layer. Save in the layer table, which is created. If you don't want to save the chamber, you can draw in the temporary layer.

axMap1.Layers._Item ( "USA Temp Layer") Editable = true;. axMap1.Layers.InsertionLayer = axMap1.Layers._Item ( "USA Temp Layer"); axMap1.CurrentTool = MapXLib.ToolConstants.miAddLineTool;

The above program uses the tool tool, which can be drawn on the temporary layer on the map on the map. The premise of these tools is the need to specify the insertlayer of AXMAP1. Can be in the layer table Insert data.

The key technologies about the layer will be introduced here. After these technologies, in the development of further research, such as the development of animation layers and drawing layers, the difficulties encountered should not be large.

Let's introduce the programming technology of C # to MAPX primitives.

9) Create a chamber on the layer:

According to the MAPX 5.0 development manual provided by Map Info, there are two ways to create a chamber, and the two code is used to implement the implementation method of the two code in C #:

First implementation method: Create a chamber directly using the Feature class

MapXLib.Style sty = new MapXLib.StyleClass (); MapXLib.Feature ftr = new MapXLib.FeatureClass (); ftr.Attach (axMap1.GetOcx ()); ftr.Type = MapXLib.FeatureTypeConstants.miFeatureTypeText; sty.TextFontColor = 255 STY.TextFont.Size = 12; ftr.style = style; ftr.caption = "new feature"; ftr.point.set (axmap1.centerx, axmap1.centery); axmap1.layers._Item ("US TOP 20 CITIES ") .Style = style; ftr = axmap1.layers._item (" US Top 20 cities "). Addfeature (FTR, New MapxLib.rowValuesClass ()); FTR.UPDATE (FTR, New MapxLib.rowValuesClass ());

There are a few places to pay attention to:

FTR.attach (Axmap1.getocx ());

This sentence should be like this in VB6:

FTR.attach Map1

From here you can see some changes brought about by strict type management. If you don't conversion, it is also wrong under VB.NET.

FTR = AXMAP1.LAYERS._ITEM ("US TOP 20 CITIES"). AddFeature (FTR, New MapxLib.rowValuesClass ()); we joined a line of air data in this program, which represents the chart we join. Does not include any data information. This is to facilitate everyone to master when introducing the primitive, do not mean that the data cannot be saved in this, in fact, by setting a new RowValues ​​variable, it is possible to put data information. Save to the primitive. We will introduce how to save data in the primary chapter.

FTR.UPDATE (FTR, New MapxLib.rowValuesClass ());

The role of this program is to save the created primitives to the layer table, which is permanently saved. Unless it is incremented on the temporary layer, when the map file is opened next time, it will be seen. The created element is still retained on the map. Therefore, back up the map files before the primary operation is used to avoid unnecessary losses.

In addition, the type (TYPE) and style of the primitive should correspond to the layer thereof, such as the type of Type and Style regarding Type and Style, and the type of "US TOP 20 CITIES" layer. Corresponding. For types and style settings, please refer to the relevant manual.

Second implementation method: create a chamber using FeatureFactory

Mapxlib.feature ftr = new mapxlib.featureclass (); mapxlib.point p = new mapxlib.pointclass (); p.set (axmap1.centerx, axmap1.centery); ftr = axmap1.layers._Item ("US top 20 cities ") .Addfeature (axmap1.featurefactory.createtext (p," new feature ", mapXlib.positionconstants.Mipomitionc, new mapxlib.styleClass ()), new mapxlib.rowvaluesclass ());

This code is similar to the above and first implementation methods, we are here no longer setup Type and Style.

FeatureFactory is similar to the Factory mode in the design mode, from it to produce various types of primitives, which is equivalent to setting the TYPE of the primitive.

10) Find Figure Yuan:

Finding the chart is very simple, you can implement the code below:

Mapxlib.findfeature Fres = NULL; FRES = AXMAP1.LAYERS._ITEM ("US Top 20 cities"). Find.Search ("New York", "); AxMap1.Centerx = Fres.centerx; axmap1.centery = FRES. CENTERY;

The above code will be placed in the middle of the map. It should be noted here that it is not possible.

Mapxlib.findfeature Fres = new mapxlib.findfeatureclass ();

Create a FindFeature object, otherwise it will be wrong:

"CLSID {436052C3-43E3-11D0-83EB-00AA00BD34FC} is invalid or unregistered." This is a bug of MapX 5.0. To avoid it, just assign NULL values ​​when you create an object.

Below we have improved the program to avoid mistakes when you can't find the primitive, and select the chamber after finding the element:

Mapxlib.Findfeature Fres = null; Fres = axmap1.layers._item ("US Top 20 cities"). Find.search ("New York", "); if (Fres.FindrC% 10 == 1) {AXMAP1. Centerx = Fres.centerx; axmap1.centery = Fres.centery; AxMap1.Layers._Item ("US TOP 20 cities"). Selection.add (Fres);} 11) Module modification:

The increase in the element, modification and deletion are non-transactional, that is, the modifications are permanently affected by the layer table data. This can make the user update the map once a plurality of primitives, but The updated effect cannot be seen before the update. We update it when you create a chamber using the first method:

FTR.UPDATE (FTR, New MapxLib.rowValuesClass ());

However, when FEATUREFACORY is created, it is not using Update, because Update is included in the related method of FeatureFactory. According to the development manual of MapX 5.0, it will be used to use Feature.Update method when modifying a chamber and updates. Other primitives replace a primitive, you should use the Layers.UpdateFeature method.

12) Delete of the element:

Like other programming languages, delete primitives use the deletefeature method, please see the MAPX online help and development manual for the introduction.

Exercise:

1. Combine the above example, and refer to the relevant information, a temporary layer is established on the map and the icon type (.bmp file) is added to the layer.

2. Customize a tool and use this tool to delete the element.

Note: Back up the map file before the chart is operated to avoid unnecessary losses.

转载请注明原文地址:https://www.9cbs.com/read-36260.html

New Post(0)