Using the Component |
|
If you wish to display data that is contained in an existing Mapinfo .MIF or ESRI .SHP file or even a TGlobe .LYR file then all you need to do is include the TGUtils unit and call one of the following: CreateFileLayer( Globe, ’test.mif’ ); CreateFileLayer( Globe, ’test.shp’ ); CreateFileLayer( Globe, ’test.lyr’ ); This will create and add a new layer to the Globe object and create a TGlobeMIFReader, TGlobeSHPReader or TGlobeLYRReader object attached to the layer that will load the data from the file. To change the way that the objects are displayed you will need to create and use a Presenter. Each object has a PresenterID property which is used to link it with a Presenter. Presenters either exist on the Layer with the data reader object or they can be added to the global presenter list. When an TGeoDataObject is rendered it’s PresenterID is used to locate a Presenter associated with the TGeoDataObject. The local Presenters on the objects layer are searched first and if no matching presenter is found then the global presenter list is searched for a match. If no Presenter is found with a matching PresenterID then a default routine is used to display the object. Objects can be created from scratch by creating a TGeoDataObject and adding it to a Layers.Objects property. The TGeoDataObject contains a Centroid property which can be set to a geographic location to represent a single point such as a city. The object's Chains property can be used to define one or more lines of points so that the object can represent rivers or country boundaries. TGlobe internally uses integers to represent latitude and longitude points. These integers have a resolution of 1/1500th of a second of Arc or it is possible to represent 2 points that are within about 1 inch or each other. These internal values are called GlobeUnits. There are a number of routines to convert to and from GlobeUnits and Decimal Degrees. The TGeoDataObject has a Chains property, this is used to store one or more lists of points. These lists of points are stored in a TPointList object. Therefore to access an individual point in a TGeoDataObject you have to specify the index of the chain that it belongs to as well as the index of the point itself. E.g. aPointLL := GeoObject.Chains.Chain[2].AsLL[5]; This will access the sixth point on the third chain of the object. This can be simplified a little as the Chain and the AsLL properties are the default properties of their respective objects so the above becomes:- aPointLL := GeoObject.Chains[2][5]; To control how an object is displayed you need to associate a Presenter with the object. The following creates a new presenter for a polyline or a polygon object and assigns the objects presenter. aBrush := TGlobeBrush.Create; aBrush.Color := clBlue; aPen := TGlobePen.Create; aPen.Color := clRed; aPresenter := TPolyPresenter.Create( Globe, 99 ); aPresenter.PolyPen.Assign( aPen ); aPresenter.PolyBrush.Assign( aBrush ); // add the Presenter to the Layer aLayer.Presenters.Add( aPresenter ); // associate the globe object with the presenter. GeoObject.PresenterID := aPresenter.PresenterID; aBrush.Free; aPen.Free; This creates a presenter with an ID of 99, the presenter is given the pen and brushes and then added to the Layer. The GeoObject should belong to the layer so that the presenter can be found. Alternatively the Presenter could be added to the Globe Presenter list like: // add the Presenter to the Global Presenter list Globe.Layers.GlobalPresenters.Add( aPresenter ); If a Presenter with an PresenterID of 99 does not exist on the Layer then this Presenter will be selected from the Global Presenter List. The following examples show how to programatically create new objects and display them on the TGlobe layers. The first example shows importing point data from an external text file, assigning a PointPresenter to the created points and then finally saving the data as a new Layer. The second example shows how to create an object and display it on a Layer as a Polygon. The important things to notice here is that in both cases a TGeoDataObject is created and used to store the longitude, latitude points. To change the default display of the object a Presenter is defined and assigned to the TGeoDataObject. This Presenter controls how the object will be rendered on the Globe, so by just changing the Presenter that is assigned to an object you can change how that object will appear. Example 1:Creating Point type objects.This routine imports data from a file (city.txt) that contains data like :- Aalborg,Denmark +57.09700 +9.85000 Abbotsford,BC +49.03000 -122.32000 Aberdeen Prv Gnd,MD +39.23500 -76.17490 The following routine then scans the file, splits up each line, and creates a TGeoDatObject for each line of data. The objects are all added to a new layer with a PointPresenter and then finally saved to a Layer file. var sName, sLon, sLat : string; idx : integer; aLayer : TGlobeLayer; slCity : TStringList; iLong, iLat : integer; aPresenter : TPointPresenter; begin { Create the Layer and a Presenter for the City data } aLayer := TGLobeLayer.Create( Globe ); aPresenter := TPointPresenter.Create( Globe, 1 ); aLayer.Presenters.Add( aPresenter ); // Add the presenter to the Layer { set up the Presenter } with aPresenter do begin PointType := ppTriangle; PointSize := 6; PointUnit := NauticalMile; TitleAlignment := taBottomRight; TitleFont.FontUnit := NauticalMile; TitleFont.FontSize := 10; end; { read and process the city data file } slCity := TStringList.Create; slCity.LoadFromFile( 'c:\windows\desktop\city.txt' ); for idx := 0 to slCity.Count - 1 do begin sName := Trim( Copy( slCity[idx], 1, 24 )); sLat := Trim( Copy( slCity[idx], 24, 18 )); sLon := Trim( Copy( slCity[idx], 42, 17 )); iLat := DecimalToGlobeUnits( StrTofloat( sLat )); iLong := DecimalToGlobeUnits( StrTofloat( sLon )); { create point objects } with TGeoDataObject.Create( Alayer.Objects ) do begin Centroid := PointLL( iLong, iLat ); Title := sName; PresenterID := aPresenter.PresenterID; end; end; { finally save the data to a .LYR file } WriteLayerToLYRfile( aLayer, 'CITY.LYR' ); end; Example 2: Creating Polygon type objects.The following routine creates a polygon, about the Equator and the Greenwich meridian. The polygon will appear on its own Layer. var aLayer : TGlobeLayer; begin aLayer := TGlobeLayer.Create( Globe1 ); { Create the object and add the points for the polygon } with TGeoDataObject.Create( aLayer.Objects ) do begin Closed := True; // So that the object will be displayed as a polygon. Chains[0].Add( PointLL( -GU_DEGREE, GU_DEGREE )); Chains[0].Add( PointLL( GU_DEGREE, GU_DEGREE )); Chains[0].Add( PointLL( GU_DEGREE, -GU_DEGREE )); Chains[0].Add( PointLL( -GU_DEGREE, -GU_DEGREE )); end; end; |
Email: tglobe@iname.com |