Skip to content

GeoAssignCoordinates

Repository source: GeoAssignCoordinates

Description

Given latitude and longitude arrays, take the values in those arrays and convert them to x,y,z world coordinates.

Uses a spherical model of the earth to do the conversion. The position is in meters relative to the center of the earth.

If a transform is given, use the transform to convert latitude and longitude to the world coordinate.

Note

The vtkGeovisCore classes as well as the module vtkViewsGeovis have been deprecated for VTK 8.2 and will be removed in a future version. See VTK Merge Request 4395

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

GeoAssignCoordinates.java

import vtk.vtkNativeLibrary;
import vtk.vtkRenderWindow;
import vtk.vtkRenderWindowInteractor;
import vtk.vtkRenderer;
import vtk.vtkActor;
import vtk.vtkMutableDirectedGraph;
import vtk.vtkDoubleArray;
import vtk.vtkGeoAssignCoordinates;
import vtk.vtkGraphMapper;

public class GeoAssignCoordinates 
{
  // -----------------------------------------------------------------
  // Load VTK library and print which library was not properly loaded
  static 
  {
    if (!vtkNativeLibrary.LoadAllNativeLibraries()) 
    {
      for (vtkNativeLibrary lib : vtkNativeLibrary.values()) 
      {
        if (!lib.IsLoaded()) 
        {
          System.out.println(lib.GetLibraryName() + " not loaded");
        }
      }
    }
    vtkNativeLibrary.DisableOutputWindow(null);
  }
  // -----------------------------------------------------------------

  public static void main(String args[]) 
  {   
    vtkMutableDirectedGraph g = new vtkMutableDirectedGraph();
    vtkDoubleArray latitude = new vtkDoubleArray();
    latitude.SetName("latitude");

    vtkDoubleArray longitude = new vtkDoubleArray();
    longitude.SetName("longitude");

    for(int lat = -90; lat <= 90; lat += 10)
    {
      for(int lon = -180; lon <= 180; lon += 20)
      {
        g.AddVertex();
        latitude.InsertNextValue(lat);
        longitude.InsertNextValue(lon);
      }
    }

    g.GetVertexData().AddArray(latitude);
    g.GetVertexData().AddArray(longitude);

    vtkGeoAssignCoordinates assign = new vtkGeoAssignCoordinates();
    assign.SetInputData(g);
    assign.SetLatitudeArrayName("latitude");
    assign.SetLongitudeArrayName("longitude");
    assign.SetGlobeRadius(1.0);
    assign.Update();

    vtkGraphMapper mapper = new vtkGraphMapper();
    mapper.SetInputConnection(assign.GetOutputPort());

    vtkActor actor = new vtkActor();
    actor.SetMapper(mapper);

    // Create the renderer, render window and interactor.
    vtkRenderer ren = new vtkRenderer();
    vtkRenderWindow renWin = new vtkRenderWindow();
    renWin.AddRenderer(ren);
    vtkRenderWindowInteractor iren = new vtkRenderWindowInteractor();
    iren.SetRenderWindow(renWin);

    ren.AddActor(actor);
    ren.ResetCamera();

    renWin.SetSize(300,300);
    renWin.Render();

    iren.Initialize();
    iren.Start();
  }
}