Skip to content


# GeoGraticle

VTKEx/Cxx/Geovis/GeoGraticle

### Description

vtkGeoGraticle generates polydata to illustrate the distortions introduced by a map projection. The level parameter specifies the number of lines to be drawn. Poles are treated differently than other regions; hence the use of a Level parameter instead of a NumberOfLines parameter. The latitude and longitude are specified as half-open intervals with units of degrees. By default the latitude bounds are (-90,90) and the longitude bounds are (0,180).

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

Other languages

See (Python)

Question

If you have a simple question about this example contact us at VTKExProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum

### Code

GeoGraticle.cxx

    #include <vtkSmartPointer.h>
    #include <vtkGeoGraticule.h>

    #include <vtkActor.h>
    #include <vtkGeoProjection.h>
    #include <vtkGeoTransform.h>
    #include <vtkPolyDataMapper.h>
    #include <vtkRenderer.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkTransformFilter.h>
    #include <vtkXMLPolyDataReader.h>

    #include <vtkNamedColors.h>

    int main( int argc, char* argv[] )
    {
      vtkSmartPointer<vtkNamedColors> colors =
        vtkSmartPointer<vtkNamedColors>::New();

      int latLevel = 2;
      int lngLevel = 2;
      const char* pname = "rouss";

      vtkSmartPointer<vtkGeoGraticule> geoGraticle =
        vtkSmartPointer<vtkGeoGraticule>::New();
      vtkSmartPointer<vtkGeoTransform> transformProjection =
        vtkSmartPointer<vtkGeoTransform>::New();
      vtkSmartPointer<vtkGeoProjection> destinationProjection =
        vtkSmartPointer<vtkGeoProjection>::New();
      vtkSmartPointer<vtkGeoProjection> sourceProjection =
        vtkSmartPointer<vtkGeoProjection>::New();
      vtkSmartPointer<vtkTransformFilter> transformGraticle =
        vtkSmartPointer<vtkTransformFilter>::New();
      vtkSmartPointer<vtkXMLPolyDataReader> reader =
        vtkSmartPointer<vtkXMLPolyDataReader>::New();
      vtkSmartPointer<vtkTransformFilter> transformReader =
        vtkSmartPointer<vtkTransformFilter>::New();
      vtkSmartPointer<vtkPolyDataMapper> graticleMapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
      vtkSmartPointer<vtkPolyDataMapper> readerMapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
      vtkSmartPointer<vtkActor> graticleActor =
        vtkSmartPointer<vtkActor>::New();
      vtkSmartPointer<vtkActor> readerActor =
        vtkSmartPointer<vtkActor>::New();

      geoGraticle->SetGeometryType( vtkGeoGraticule::POLYLINES );
      geoGraticle->SetLatitudeLevel( latLevel );
      geoGraticle->SetLongitudeLevel( lngLevel );
      geoGraticle->SetLongitudeBounds( -180, 180 );
      geoGraticle->SetLatitudeBounds( -90, 90 );

      // destinationProjection defaults to latlong.
      destinationProjection->SetName( pname );
      destinationProjection->SetCentralMeridian( 0. );
      transformProjection->SetSourceProjection( sourceProjection );
      transformProjection->SetDestinationProjection( destinationProjection );
      transformGraticle->SetInputConnection( geoGraticle->GetOutputPort() );
      transformGraticle->SetTransform( transformProjection );
      graticleMapper->SetInputConnection( transformGraticle->GetOutputPort() );
      graticleActor->SetMapper( graticleMapper );

      reader->SetFileName(argv[1]);
      transformReader->SetTransform( transformProjection );
      transformReader->SetInputConnection( reader->GetOutputPort() );
      readerMapper->SetInputConnection( transformReader->GetOutputPort() );
      readerActor->SetMapper( readerMapper );

      vtkSmartPointer<vtkRenderWindow> renderWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
      vtkSmartPointer<vtkRenderer> renderer =
        vtkSmartPointer<vtkRenderer>::New();
      vtkSmartPointer<vtkRenderWindowInteractor> interactor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
      renderWindow->SetInteractor( interactor );
      renderWindow->AddRenderer( renderer );
      renderWindow->SetSize(640, 480);
      renderer->SetBackground(colors->GetColor3d("BurlyWood").GetData());

      renderer->AddActor( readerActor );
      renderer->AddActor( graticleActor );

      renderWindow->Render();

      interactor->Initialize();
      interactor->Start();
      return EXIT_SUCCESS;
    };

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(GeoGraticle)

    find_package(VTK COMPONENTS
      vtkCommonColor
      vtkCommonCore
      vtkFiltersGeneral
      vtkGeovisCore
      vtkIOXML
      vtkInteractionStyle
      vtkRenderingContextOpenGL2
      vtkRenderingCore
      vtkRenderingFreeType
      vtkRenderingGL2PSOpenGL2
      vtkRenderingOpenGL2 QUIET)
    if (NOT VTK_FOUND)
      message("Skipping GeoGraticle: ${VTK_NOT_FOUND_MESSAGE}")
      return ()
    endif()
    message (STATUS "VTK_VERSION: ${VTK_VERSION}")
    if (VTK_VERSION VERSION_LESS "8.90.0")
      # old system
      include(${VTK_USE_FILE})
      add_executable(GeoGraticle MACOSX_BUNDLE GeoGraticle.cxx )
      target_link_libraries(GeoGraticle PRIVATE ${VTK_LIBRARIES})
    else ()
      # include all components
      add_executable(GeoGraticle MACOSX_BUNDLE GeoGraticle.cxx )
      target_link_libraries(GeoGraticle PRIVATE ${VTK_LIBRARIES})
      # vtk_module_autoinit is needed
      vtk_module_autoinit(
        TARGETS GeoGraticle
        MODULES ${VTK_LIBRARIES}
        )
    endif ()

### Download and Build GeoGraticle

Click here to download GeoGraticle and its CMakeLists.txt file. Once the tarball GeoGraticle.tar has been downloaded and extracted,

    cd GeoGraticle/build

If VTK is installed:

    cmake ..

If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:

    cmake -DVTK_DIR:PATH=/home/me/vtk_build ..

Build the project:

    make

and run it:

    ./GeoGraticle

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.