├── .gitignore ├── 0_hello_world_qt_style ├── CMakeLists.txt ├── COPYING ├── README ├── main.cpp └── tutorial1.t2t ├── 1_hello_world_qgis_style ├── CMakeLists.txt ├── COPYING ├── README ├── cmake_find_rules │ ├── FindGDAL.cmake │ ├── FindGEOS.cmake │ └── FindQGIS.cmake ├── images │ └── tutorial1.jpg ├── main.cpp └── tutorial1.t2t ├── 2_basic_main_window ├── CMakeLists.txt ├── COPYING ├── README ├── cmake_find_rules │ ├── FindGDAL.cmake │ ├── FindGEOS.cmake │ └── FindQGIS.cmake ├── data │ ├── test.dbf │ ├── test.prj │ ├── test.shp │ └── test.shx ├── images │ └── tutorial2.jpg ├── mActionAddLayer.png ├── mActionPan.png ├── mActionZoomIn.png ├── mActionZoomOut.png ├── main.cpp ├── mainwindow.cpp ├── mainwindow.h ├── mainwindowbase.ui ├── resources.qrc └── tutorial2.t2t ├── 3_basic_labelling ├── CMakeLists.txt ├── COPYING ├── README ├── cmake_find_rules │ ├── FindGDAL.cmake │ ├── FindGEOS.cmake │ └── FindQGIS.cmake ├── data │ ├── test.dbf │ ├── test.prj │ ├── test.shp │ └── test.shx ├── mActionAddLayer.png ├── mActionPan.png ├── mActionZoomIn.png ├── mActionZoomOut.png ├── main.cpp ├── mainwindow.cpp ├── mainwindow.h ├── mainwindowbase.ui ├── resources.qrc └── tutorial3.t2t ├── 4_adding_rasters_to_canvas ├── 4_adding_rasters_to_canvas.pro ├── CMakeLists.txt ├── build.mac.sh ├── cmake_find_rules │ ├── FindGDAL.cmake │ ├── FindGEOS.cmake │ └── FindQGIS.cmake ├── data │ └── Abarema_jupunba_projection.tif ├── mActionAddLayer.png ├── mActionPan.png ├── mActionZoomIn.png ├── mActionZoomOut.png ├── main.cpp ├── mainwindow.cpp ├── mainwindow.h ├── mainwindowbase.ui ├── resources.qrc └── tutorial4.html ├── 5_using_rubber_band_with_canvas ├── CMakeLists.txt ├── cmake_find_rules │ ├── FindGDAL.cmake │ ├── FindGEOS.cmake │ └── FindQGIS.cmake ├── data │ └── Abarema_jupunba_projection.tif ├── mActionAddLayer.png ├── mActionPan.png ├── mActionZoomIn.png ├── mActionZoomOut.png ├── main.cpp ├── mainwindow.cpp ├── mainwindow.h ├── mainwindowbase.ui ├── resources.qrc └── tutorial5.html ├── 6_accessing_vector_attributes ├── 6_accessing_vector_attributes.pro ├── data │ ├── test.dbf │ ├── test.prj │ ├── test.shp │ └── test.shx ├── main.cpp ├── mainwindow.cpp ├── mainwindow.h ├── mainwindowbase.ui ├── resources.qrc └── tutorial6.html ├── 7_writing_custom_maptools ├── 7_writing_custom_maptools.pro ├── data │ └── Abarema_jupunba_projection.tif ├── mActionAddLayer.png ├── mActionPan.png ├── mActionZoomIn.png ├── mActionZoomOut.png ├── main.cpp ├── mainwindow.cpp ├── mainwindow.h ├── mainwindowbase.ui ├── maptooldriller.cpp ├── maptooldriller.h ├── resources.qrc └── tutorial7.html ├── README ├── generate_docs.sh ├── images ├── tim50x50.png ├── tutorial1.jpg ├── tutorial2.jpg └── tutorial3.jpg ├── index.t2t ├── plugin_writer_workshop ├── plugin_writer.pdf ├── pointconverter_final │ ├── plugin_writer.pdf │ ├── pointconverter.pro │ ├── qgspointconverterplugin.cpp │ └── qgspointconverterplugin.h ├── pointconverter_step1 │ ├── Makefile │ ├── libpointconverter.so.1.0.0 │ ├── pointconverter.pro │ ├── qgspointconverterplugin.cpp │ └── qgspointconverterplugin.h ├── pointconverter_step2 │ ├── Makefile │ ├── libpointconverter.so.1.0.0 │ ├── moc_qgspointconverterplugin.cpp │ ├── pointconverter.pro │ ├── qgspointconverterplugin.cpp │ └── qgspointconverterplugin.h └── pointconverter_step3 │ ├── Makefile │ ├── libpointconverter.so.1.0.0 │ ├── moc_qgspointconverterplugin.cpp │ ├── pointconverter.pro │ ├── qgspointconverterplugin.cpp │ └── qgspointconverterplugin.h └── style.css /.gitignore: -------------------------------------------------------------------------------- 1 | cmake-build-debug/* 2 | .idea 3 | -------------------------------------------------------------------------------- /0_hello_world_qt_style/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | 3 | PROJECT(timtut0) 4 | SET(CMAKE_COLOR_MAKEFILE ON) 5 | 6 | # For mac - hard code path to Qt5 - normally done using 7 | # -DCMAKE_PREFIX_PATH 8 | SET(CMAKE_PREFIX_PATH /usr/local/opt/qt5 /usr/local/opt/qt5-webkit) 9 | # set path to additional CMake modules 10 | SET(CMAKE_MODULE_PATH 11 | ${CMAKE_CURRENT_SOURCE_DIR}/cmake_find_rules 12 | ${CMAKE_MODULE_PATH}) 13 | 14 | # Tell CMake to run moc when necessary: 15 | set(CMAKE_AUTOMOC ON) 16 | # As moc files are generated in the binary dir, tell CMake 17 | # to always look for includes there: 18 | set(CMAKE_INCLUDE_CURRENT_DIR ON) 19 | 20 | # Widgets finds its own dependencies. 21 | find_package(Qt5Widgets REQUIRED) 22 | 23 | add_executable(hello_world main.cpp) 24 | 25 | qt5_use_modules(hello_world Widgets) 26 | 27 | 28 | -------------------------------------------------------------------------------- /0_hello_world_qt_style/README: -------------------------------------------------------------------------------- 1 | This tutorial is copyright Tim Sutton, 2006, 2008, 2017. 2 | 3 | This work is licensed under the GPL version 2 - see COPYING file for more details. 4 | -------------------------------------------------------------------------------- /0_hello_world_qt_style/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Qt Includes 3 | // 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main(int argc, char ** argv) 10 | { 11 | // Start the Application 12 | QApplication app(argc, argv); 13 | QWidget widget; 14 | QVBoxLayout layout; 15 | widget.setLayout (&layout); 16 | 17 | QLabel label(QStringLiteral("Hello World"), &widget); 18 | QLabel *label2 = new QLabel(QStringLiteral ("Goodbye World"), &widget); 19 | 20 | layout.addWidget(&label); 21 | layout.addWidget(label2); 22 | 23 | widget.show(); 24 | 25 | label.show(); 26 | // Start the Application Event Loop 27 | return app.exec(); 28 | } 29 | 30 | 31 | -------------------------------------------------------------------------------- /0_hello_world_qt_style/tutorial1.t2t: -------------------------------------------------------------------------------- 1 | QGIS Developer Tutorials 2 | 3 | Tim Sutton, 2008 4 | 5 | %! target : html 6 | %! style : style.css 7 | %! Options : --toc --toc-level 3 --enum-title --css-sugar --css-inside 8 | %! preproc : TUT_URL https://qgis.org 9 | %! PostProc(html): '(?i)(```)' '
\1' 10 | %! PostProc(html): '(?i)(```)' '\1
' 11 | %! encoding: iso-8859-1 12 | % These are comments and will not be generated in any output 13 | % ------------------- 14 | %This document is in text2tags format. You can generate html, plain text and 15 | %moinmoin formatted documentation by running txt2tags on this document. See the 16 | %txt2tags home page for more details. Please insert manual line breaks in this 17 | %document as it makes diffing for changes much easier. To do this in vim 18 | %automatically, select a section then issue (gq) command. Please dont 19 | %apply vim formatting to the whol document as it screws up some formatting 20 | %rather apply it selectively to paragraphs where needed. 21 | % To generate the text version of this document: 22 | % 23 | % Make sure you are in the top level code examples dir 24 | % since image paths are referenced from there: 25 | % 26 | % txt2tags -t txt -o tutorial1 1_hello_world_qgis_style/tutorial1.t2t 27 | % To generate the moinmoin version of this document 28 | % txt2tags -t moin -o tutorial1.moin 1_hello_world_qgis_style/tutorial1.t2t 29 | % To generate the html version of this document 30 | % txt2tags -t html -o tutorial1.html 1_hello_world_qgis_style/tutorial1.t2t 31 | % End of comments 32 | % ------------------- 33 | 34 | = Tutorial 0 - Hello World, Qt style = 35 | 36 | 37 | -------------------------------------------------------------------------------- /1_hello_world_qgis_style/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.6) 2 | PROJECT(timtut1) 3 | SET(CMAKE_COLOR_MAKEFILE ON) 4 | # set path to additional CMake modules 5 | SET(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake_find_rules ${CMAKE_MODULE_PATH}) 6 | FIND_PACKAGE(QGIS REQUIRED) 7 | IF (NOT QGIS_FOUND) 8 | MESSAGE (SEND_ERROR "QGIS dependency was not found!") 9 | ENDIF (NOT QGIS_FOUND) 10 | IF (WIN32) 11 | # expect that classes are being imported by default 12 | # Note: MSVC doesn't like when the macros are quotes 13 | # and MSYS doesn't like them unqouted (bacause of braces) 14 | # import qgis classes 15 | IF (MSVC) 16 | ADD_DEFINITIONS("-DGUI_EXPORT=__declspec(dllimport)") 17 | ADD_DEFINITIONS("-DCORE_EXPORT=__declspec(dllimport)") 18 | ELSE (MSVC) 19 | ADD_DEFINITIONS("\"-DGUI_EXPORT=__declspec(dllimport)\"") 20 | ADD_DEFINITIONS("\"-DCORE_EXPORT=__declspec(dllimport)\"") 21 | ENDIF (MSVC) 22 | ELSE (WIN32) 23 | ADD_DEFINITIONS(-DGUI_EXPORT=) 24 | ADD_DEFINITIONS(-DCORE_EXPORT=) 25 | ENDIF (WIN32) 26 | 27 | FIND_PACKAGE(GDAL REQUIRED) 28 | IF (NOT GDAL_FOUND) 29 | MESSAGE (SEND_ERROR "GDAL dependency was not found!") 30 | ENDIF (NOT GDAL_FOUND) 31 | 32 | FIND_PACKAGE(GEOS REQUIRED) 33 | IF (NOT GEOS_FOUND) 34 | MESSAGE (SEND_ERROR "GEOS dependency was not found!") 35 | ENDIF (NOT GEOS_FOUND) 36 | 37 | IF (CMAKE_BUILD_TYPE MATCHES Debug) 38 | ADD_DEFINITIONS(-Dtimtut1DEBUG=1) 39 | ENDIF (CMAKE_BUILD_TYPE MATCHES Debug) 40 | ######################################################## 41 | # Files 42 | 43 | SET (timtut1_SRCS 44 | main.cpp 45 | ) 46 | 47 | # This tut uses no UIs 48 | SET (timtut1_UIS 49 | ) 50 | 51 | # This tut needs no MOC 52 | SET (timtut1_MOC_HDRS 53 | ) 54 | 55 | # This tut uses no resource files 56 | SET (timtut1_RCCS 57 | 58 | ) 59 | 60 | SET (QT_USE_QT3SUPPORT FALSE) 61 | SET (QT_USE_QTGUI TRUE) 62 | SET (QT_USE_QTSQL TRUE) 63 | SET (QT_USE_QTSVG TRUE) 64 | SET (QT_USE_QTXML TRUE) 65 | SET (QT_USE_QTNETWORK TRUE) 66 | FIND_PACKAGE(Qt4 REQUIRED) 67 | INCLUDE( ${QT_USE_FILE} ) 68 | ######################################################## 69 | # Build 70 | 71 | QT4_WRAP_UI (timtut1_UIS_H ${timtut1_UIS}) 72 | 73 | QT4_WRAP_CPP (timtut1_MOC_SRCS ${timtut1_MOC_HDRS}) 74 | 75 | QT4_ADD_RESOURCES(timtut1_RCC_SRCS ${timtut1_RCCS}) 76 | 77 | ADD_EXECUTABLE (timtut1 ${timtut1_SRCS} ${timtut1_MOC_SRCS} ${timtut1_RCC_SRCS} ${timtut1_UIS_H}) 78 | 79 | INCLUDE_DIRECTORIES( 80 | ${GDAL_INCLUDE_DIR} 81 | ${GEOS_INCLUDE_DIR} 82 | ${CMAKE_CURRENT_BINARY_DIR} 83 | ${QT_INCLUDE_DIR} 84 | ${QGIS_INCLUDE_DIR} 85 | . 86 | ) 87 | 88 | #This is probably no longer needed, but I will leave it in for Win machines for the moment 89 | IF(QT_QTSQL_FOUND) 90 | FIND_LIBRARY(QT_QTSQL_LIBRARY NAMES QtSql QtSql4 PATHS ${QT_LIBRARY_DIR} NO_DEFAULT_PATH) 91 | SET(QT_LIBRARIES ${QT_LIBRARIES} ${QT_QTSQL_LIBRARY}) 92 | ENDIF(QT_QTSQL_FOUND) 93 | 94 | TARGET_LINK_LIBRARIES(timtut1 95 | ${QT_LIBRARIES} 96 | ${QGIS_CORE_LIBRARY} 97 | ${QGIS_GUI_LIBRARY} 98 | ${GEOS_LIBRARY} 99 | ${GDAL_LIBRARY} 100 | ) 101 | 102 | IF (MSVC) 103 | # Very important or you get all kinds of odd side 104 | #effects like app crash on start up saying qtgui.dll 105 | TARGET_LINK_LIBRARIES( timtut1 106 | qtmain 107 | ) 108 | ENDIF (MSVC) 109 | 110 | ######################################################## 111 | # Install 112 | 113 | 114 | -------------------------------------------------------------------------------- /1_hello_world_qgis_style/README: -------------------------------------------------------------------------------- 1 | This tutorial is copyright Tim Sutton, 2006, 2008. 2 | 3 | This work is licensed under the GPL version 2 - see COPYING file for more details. 4 | -------------------------------------------------------------------------------- /1_hello_world_qgis_style/cmake_find_rules/FindGDAL.cmake: -------------------------------------------------------------------------------- 1 | ## Once run this will define: 2 | ## 3 | ## GDAL_FOUND = system has GDAL lib 4 | ## 5 | ## GDAL_LIBRARY = full path to the library 6 | ## 7 | ## GDAL_INCLUDE_DIR = where to find headers 8 | ## 9 | ## Magnus Homann 10 | 11 | 12 | IF(WIN32) 13 | 14 | IF (MINGW) 15 | FIND_PATH(GDAL_INCLUDE_DIR gdal.h /usr/local/include /usr/include c:/msys/local/include) 16 | FIND_LIBRARY(GDAL_LIBRARY NAMES gdal PATHS /usr/local/lib /usr/lib c:/msys/local/lib) 17 | ENDIF (MINGW) 18 | 19 | IF (MSVC) 20 | FIND_PATH(GDAL_INCLUDE_DIR gdal.h 21 | "$ENV{LIB_DIR}/include/gdal" 22 | ) 23 | FIND_LIBRARY(GDAL_LIBRARY NAMES gdal PATHS 24 | "$ENV{LIB_DIR}/lib/" 25 | ) 26 | 27 | # NOTE: under msvc you should add the following to your target link libraries 28 | # list as they are required by gdal 29 | #odbc32 odbccp32 30 | 31 | ENDIF (MSVC) 32 | 33 | 34 | ELSE(WIN32) 35 | IF(UNIX) 36 | 37 | # try to use framework on mac 38 | IF (APPLE) 39 | SET (GDAL_MAC_PATH /Library/Frameworks/GDAL.framework/unix/bin) 40 | ENDIF (APPLE) 41 | 42 | SET(GDAL_CONFIG_PREFER_PATH "$ENV{GDAL_HOME}/bin" CACHE STRING "preferred path to GDAL (gdal-config)") 43 | FIND_PROGRAM(GDAL_CONFIG gdal-config 44 | ${GDAL_CONFIG_PREFER_PATH} 45 | ${GDAL_MAC_PATH} 46 | /usr/local/bin/ 47 | /usr/bin/ 48 | ) 49 | # MESSAGE("DBG GDAL_CONFIG ${GDAL_CONFIG}") 50 | 51 | IF (GDAL_CONFIG) 52 | # set INCLUDE_DIR to prefix+include 53 | EXEC_PROGRAM(${GDAL_CONFIG} 54 | ARGS --prefix 55 | OUTPUT_VARIABLE GDAL_PREFIX) 56 | #SET(GDAL_INCLUDE_DIR ${GDAL_PREFIX}/include CACHE STRING INTERNAL) 57 | FIND_PATH(GDAL_INCLUDE_DIR 58 | gdal.h 59 | ${GDAL_PREFIX}/include/gdal 60 | ${GDAL_PREFIX}/include 61 | /usr/local/include 62 | /usr/include 63 | ) 64 | 65 | ## extract link dirs for rpath 66 | EXEC_PROGRAM(${GDAL_CONFIG} 67 | ARGS --libs 68 | OUTPUT_VARIABLE GDAL_CONFIG_LIBS ) 69 | 70 | ## split off the link dirs (for rpath) 71 | ## use regular expression to match wildcard equivalent "-L*" 72 | ## with is a space or a semicolon 73 | STRING(REGEX MATCHALL "[-][L]([^ ;])+" 74 | GDAL_LINK_DIRECTORIES_WITH_PREFIX 75 | "${GDAL_CONFIG_LIBS}" ) 76 | # MESSAGE("DBG GDAL_LINK_DIRECTORIES_WITH_PREFIX=${GDAL_LINK_DIRECTORIES_WITH_PREFIX}") 77 | 78 | ## remove prefix -L because we need the pure directory for LINK_DIRECTORIES 79 | 80 | IF (GDAL_LINK_DIRECTORIES_WITH_PREFIX) 81 | STRING(REGEX REPLACE "[-][L]" "" GDAL_LINK_DIRECTORIES ${GDAL_LINK_DIRECTORIES_WITH_PREFIX} ) 82 | ENDIF (GDAL_LINK_DIRECTORIES_WITH_PREFIX) 83 | 84 | ## split off the name 85 | ## use regular expression to match wildcard equivalent "-l*" 86 | ## with is a space or a semicolon 87 | STRING(REGEX MATCHALL "[-][l]([^ ;])+" 88 | GDAL_LIB_NAME_WITH_PREFIX 89 | "${GDAL_CONFIG_LIBS}" ) 90 | # MESSAGE("DBG GDAL_LIB_NAME_WITH_PREFIX=${GDAL_LIB_NAME_WITH_PREFIX}") 91 | 92 | 93 | ## remove prefix -l because we need the pure name 94 | 95 | IF (GDAL_LIB_NAME_WITH_PREFIX) 96 | STRING(REGEX REPLACE "[-][l]" "" GDAL_LIB_NAME ${GDAL_LIB_NAME_WITH_PREFIX} ) 97 | ENDIF (GDAL_LIB_NAME_WITH_PREFIX) 98 | 99 | IF (APPLE) 100 | SET(GDAL_LIBRARY ${GDAL_LINK_DIRECTORIES}/lib${GDAL_LIB_NAME}.dylib CACHE STRING INTERNAL) 101 | ELSE (APPLE) 102 | SET(GDAL_LIBRARY ${GDAL_LINK_DIRECTORIES}/lib${GDAL_LIB_NAME}.so CACHE STRING INTERNAL) 103 | ENDIF (APPLE) 104 | 105 | ELSE(GDAL_CONFIG) 106 | MESSAGE("FindGDAL.cmake: gdal-config not found. Please set it manually. GDAL_CONFIG=${GDAL_CONFIG}") 107 | ENDIF(GDAL_CONFIG) 108 | 109 | ENDIF(UNIX) 110 | ENDIF(WIN32) 111 | 112 | 113 | IF (GDAL_INCLUDE_DIR AND GDAL_LIBRARY) 114 | SET(GDAL_FOUND TRUE) 115 | ENDIF (GDAL_INCLUDE_DIR AND GDAL_LIBRARY) 116 | 117 | IF (GDAL_FOUND) 118 | 119 | IF (NOT GDAL_FIND_QUIETLY) 120 | MESSAGE(STATUS "Found GDAL: ${GDAL_LIBRARY}") 121 | ENDIF (NOT GDAL_FIND_QUIETLY) 122 | 123 | ELSE (GDAL_FOUND) 124 | 125 | MESSAGE(GDAL_INCLUDE_DIR=${GDAL_INCLUDE_DIR}) 126 | MESSAGE(GDAL_LIBRARY=${GDAL_LIBRARY}) 127 | MESSAGE(FATAL_ERROR "Could not find GDAL") 128 | 129 | ENDIF (GDAL_FOUND) 130 | -------------------------------------------------------------------------------- /1_hello_world_qgis_style/cmake_find_rules/FindGEOS.cmake: -------------------------------------------------------------------------------- 1 | 2 | # CMake module to search for GEOS library 3 | # 4 | # If it's found it sets GEOS_FOUND to TRUE 5 | # and following variables are set: 6 | # GEOS_INCLUDE_DIR 7 | # GEOS_LIBRARY 8 | 9 | 10 | FIND_PATH(GEOS_INCLUDE_DIR geos.h 11 | /usr/local/include 12 | /usr/include 13 | #MSVC 14 | "$ENV{LIB_DIR}/include" 15 | #mingw 16 | c:/msys/local/include 17 | ) 18 | 19 | FIND_LIBRARY(GEOS_LIBRARY NAMES geos PATHS 20 | /usr/local/lib 21 | /usr/lib 22 | #MSVC 23 | "$ENV{LIB_DIR}/lib" 24 | #mingw 25 | c:/msys/local/lib 26 | ) 27 | 28 | IF (GEOS_INCLUDE_DIR AND GEOS_LIBRARY) 29 | SET(GEOS_FOUND TRUE) 30 | ENDIF (GEOS_INCLUDE_DIR AND GEOS_LIBRARY) 31 | 32 | 33 | IF (GEOS_FOUND) 34 | 35 | IF (NOT GEOS_FIND_QUIETLY) 36 | MESSAGE(STATUS "Found GEOS: ${GEOS_LIBRARY}") 37 | ENDIF (NOT GEOS_FIND_QUIETLY) 38 | 39 | ELSE (GEOS_FOUND) 40 | 41 | IF (GEOS_FIND_REQUIRED) 42 | MESSAGE(FATAL_ERROR "Could not find GEOS") 43 | ENDIF (GEOS_FIND_REQUIRED) 44 | 45 | ENDIF (GEOS_FOUND) 46 | -------------------------------------------------------------------------------- /1_hello_world_qgis_style/cmake_find_rules/FindQGIS.cmake: -------------------------------------------------------------------------------- 1 | ## Once run this will define: 2 | ## 3 | ## QGIS_FOUND = system has QGIS lib 4 | ## 5 | ## QGIS_CORE_LIBRARY = full path to the CORE library 6 | ## QGIS_GUI_LIBRARY = full path to the GUI library 7 | ## QGIS_PLUGIN_DIR = full path to where QGIS plugins are installed 8 | ## QGIS_INCLUDE_DIR = where to find headers 9 | ## 10 | ## Tim Sutton 11 | 12 | #MESSAGE("Searching for QGIS") 13 | IF(WIN32) 14 | #MESSAGE("Searching for QGIS in C:/program files/Quantum GIS") 15 | IF (MINGW) 16 | FIND_PATH(QGIS_PLUGIN_DIR libnortharrowplugin.dll 17 | "C:/Program Files/Quantum GIS/plugins" 18 | ) 19 | FIND_PATH(QGIS_INCLUDE_DIR qgsapplication.h 20 | "C:/Program Files/Quantum GIS/include" 21 | ) 22 | FIND_LIBRARY(QGIS_CORE_LIBRARY NAMES qgis_core PATHS 23 | "C:/Program Files/Quantum GIS/" 24 | ) 25 | FIND_LIBRARY(QGIS_GUI_LIBRARY NAMES qgis_gui PATHS 26 | "C:/Program Files/Quantum GIS/" 27 | ) 28 | ENDIF (MINGW) 29 | 30 | IF (MSVC) 31 | FIND_PATH(QGIS_PLUGIN_DIR libnortharrowplugin.dll 32 | "C:/Program Files/Quantum GIS/lib/qgis" 33 | ) 34 | FIND_PATH(QGIS_INCLUDE_DIR qgsapplication.h 35 | "$ENV{LIB_DIR}/include/qgis" 36 | ) 37 | FIND_LIBRARY(QGIS_CORE_LIBRARY NAMES qgis_core PATHS 38 | "$ENV{LIB_DIR}/lib/" 39 | ) 40 | FIND_LIBRARY(QGIS_GUI_LIBRARY NAMES qgis_gui PATHS 41 | "$ENV{LIB_DIR}/lib/" 42 | ) 43 | ENDIF (MSVC) 44 | 45 | ELSE(WIN32) 46 | IF(UNIX) 47 | 48 | # try to use bundle on mac 49 | IF (APPLE) 50 | #MESSAGE("Searching for QGIS in /Applications/QGIS.app/Contents/MacOS") 51 | SET (QGIS_MAC_PATH /Applications/QGIS1.0.0.app/Contents/MacOS) 52 | SET (QGIS_LIB_DIR ${QGIS_MAC_PATH}/lib) 53 | SET (QGIS_PLUGIN_DIR ${QGIS_MAC_PATH}/lib/qgis CACHE STRING INTERNAL) 54 | # set INCLUDE_DIR to prefix+include 55 | SET(QGIS_INCLUDE_DIR ${QGIS_MAC_PATH}/include/qgis CACHE STRING INTERNAL) 56 | ## extract link dirs 57 | SET(QGIS_CORE_LIBRARY ${QGIS_LIB_DIR}/libqgis_core.dylib CACHE STRING INTERNAL) 58 | SET(QGIS_GUI_LIBRARY ${QGIS_LIB_DIR}/libqgis_gui.dylib CACHE STRING INTERNAL) 59 | ELSE (APPLE) # Unix / Linux 60 | #MESSAGE("Searching for QGIS in /usr/bin; /usr/local/bin") 61 | FIND_PATH(QGIS_PLUGIN_DIR libnortharrowplugin.so 62 | /usr/lib/qgis 63 | /usr/local/lib/qgis 64 | "$ENV{LIB_DIR}/lib/qgis" 65 | ) 66 | FIND_PATH(QGIS_INCLUDE_DIR qgis.h 67 | /usr/include/qgis 68 | /usr/local/include/qgis 69 | "$ENV{LIB_DIR}/include/qgis" 70 | ) 71 | FIND_LIBRARY(QGIS_CORE_LIBRARY NAMES qgis_core PATHS 72 | /usr/lib 73 | /usr/local/lib 74 | "$ENV{LIB_DIR}/lib" 75 | ) 76 | FIND_LIBRARY(QGIS_GUI_LIBRARY NAMES qgis_gui PATHS 77 | /usr/lib 78 | /usr/local/lib 79 | "$ENV{LIB_DIR}/lib" 80 | ) 81 | ENDIF (APPLE) 82 | ENDIF(UNIX) 83 | ENDIF(WIN32) 84 | 85 | 86 | IF (QGIS_INCLUDE_DIR AND QGIS_CORE_LIBRARY AND QGIS_GUI_LIBRARY) 87 | SET(QGIS_FOUND TRUE) 88 | ENDIF (QGIS_INCLUDE_DIR AND QGIS_CORE_LIBRARY AND QGIS_GUI_LIBRARY) 89 | 90 | IF (QGIS_FOUND) 91 | IF (NOT QGIS_FIND_QUIETLY) 92 | MESSAGE(STATUS "Found QGIS Core: ${QGIS_CORE_LIBRARY}") 93 | MESSAGE(STATUS "Found QGIS Gui: ${QGIS_GUI_LIBRARY}") 94 | MESSAGE(STATUS "Found QGIS Plugins Dir: ${QGIS_PLUGIN_DIR}") 95 | ENDIF (NOT QGIS_FIND_QUIETLY) 96 | ELSE (QGIS_FOUND) 97 | IF (QGIS_FIND_REQUIRED) 98 | MESSAGE(FATAL_ERROR "Could not find QGIS") 99 | ENDIF (QGIS_FIND_REQUIRED) 100 | ENDIF (QGIS_FOUND) 101 | -------------------------------------------------------------------------------- /1_hello_world_qgis_style/images/tutorial1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/1_hello_world_qgis_style/images/tutorial1.jpg -------------------------------------------------------------------------------- /1_hello_world_qgis_style/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // QGIS Includes 3 | // 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | // 11 | // Qt Includes 12 | // 13 | #include 14 | #include 15 | #include 16 | 17 | int main(int argc, char ** argv) 18 | { 19 | // Start the Application 20 | QgsApplication app(argc, argv, true); 21 | 22 | QString myPluginsDir = "/home/timlinux/apps/lib/qgis"; 23 | QString myLayerPath = "/home/timlinux/gisdata/brazil/BR_Cidades/"; 24 | QString myLayerBaseName = "Brasil_Cap"; 25 | QString myProviderName = "ogr"; 26 | // Instantiate Provider Registry 27 | QgsProviderRegistry::instance(myPluginsDir); 28 | // create a maplayer instance 29 | QgsVectorLayer * mypLayer = 30 | new QgsVectorLayer(myLayerPath, myLayerBaseName, myProviderName); 31 | QgsSingleSymbolRenderer *mypRenderer = new QgsSingleSymbolRenderer(mypLayer->geometryType()); 32 | QList myLayerSet; 33 | mypLayer->setRenderer(mypRenderer); 34 | if (mypLayer->isValid()) 35 | { 36 | qDebug("Layer is valid"); 37 | } 38 | else 39 | { 40 | qDebug("Layer is NOT valid"); 41 | } 42 | 43 | // Add the Vector Layer to the Layer Registry 44 | QgsMapLayerRegistry::instance()->addMapLayer(mypLayer, TRUE); 45 | // Add the Layer to the Layer Set 46 | myLayerSet.append(QgsMapCanvasLayer(mypLayer, TRUE)); 47 | 48 | // Create the Map Canvas 49 | QgsMapCanvas * mypMapCanvas = new QgsMapCanvas(0, 0); 50 | mypMapCanvas->setExtent(mypLayer->extent()); 51 | mypMapCanvas->enableAntiAliasing(true); 52 | mypMapCanvas->setCanvasColor(QColor(255, 255, 255)); 53 | mypMapCanvas->freeze(false); 54 | // Set the Map Canvas Layer Set 55 | mypMapCanvas->setLayerSet(myLayerSet); 56 | mypMapCanvas->setVisible(true); 57 | mypMapCanvas->refresh(); 58 | 59 | // Start the Application Event Loop 60 | return app.exec(); 61 | } 62 | 63 | 64 | -------------------------------------------------------------------------------- /2_basic_main_window/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.6) 2 | PROJECT(timtut2) 3 | SET(CMAKE_COLOR_MAKEFILE ON) 4 | # set path to additional CMake modules 5 | SET(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake_find_rules ${CMAKE_MODULE_PATH}) 6 | FIND_PACKAGE(QGIS REQUIRED) 7 | IF (NOT QGIS_FOUND) 8 | MESSAGE (SEND_ERROR "QGIS dependency was not found!") 9 | ENDIF (NOT QGIS_FOUND) 10 | IF (WIN32) 11 | # expect that classes are being imported by default 12 | # Note: MSVC doesn't like when the macros are quotes 13 | # and MSYS doesn't like them unqouted (bacause of braces) 14 | # import qgis classes 15 | IF (MSVC) 16 | ADD_DEFINITIONS("-DGUI_EXPORT=__declspec(dllimport)") 17 | ADD_DEFINITIONS("-DCORE_EXPORT=__declspec(dllimport)") 18 | ELSE (MSVC) 19 | ADD_DEFINITIONS("\"-DGUI_EXPORT=__declspec(dllimport)\"") 20 | ADD_DEFINITIONS("\"-DCORE_EXPORT=__declspec(dllimport)\"") 21 | ENDIF (MSVC) 22 | ELSE (WIN32) 23 | ADD_DEFINITIONS(-DGUI_EXPORT=) 24 | ADD_DEFINITIONS(-DCORE_EXPORT=) 25 | ENDIF (WIN32) 26 | 27 | FIND_PACKAGE(GDAL REQUIRED) 28 | IF (NOT GDAL_FOUND) 29 | MESSAGE (SEND_ERROR "GDAL dependency was not found!") 30 | ENDIF (NOT GDAL_FOUND) 31 | 32 | FIND_PACKAGE(GEOS REQUIRED) 33 | IF (NOT GEOS_FOUND) 34 | MESSAGE (SEND_ERROR "GEOS dependency was not found!") 35 | ENDIF (NOT GEOS_FOUND) 36 | 37 | IF (CMAKE_BUILD_TYPE MATCHES Debug) 38 | ADD_DEFINITIONS(-Dtimtut2DEBUG=1) 39 | ENDIF (CMAKE_BUILD_TYPE MATCHES Debug) 40 | ######################################################## 41 | # Files 42 | 43 | SET (timtut2_SRCS 44 | main.cpp 45 | mainwindow.cpp 46 | ) 47 | 48 | # This tut uses no UIs 49 | SET (timtut2_UIS 50 | mainwindowbase.ui 51 | ) 52 | 53 | # This tut needs no MOC 54 | SET (timtut2_MOC_HDRS 55 | mainwindow.h 56 | ) 57 | 58 | 59 | SET (timtut2_RCCS 60 | resources.qrc 61 | ) 62 | 63 | SET (QT_USE_QT3SUPPORT FALSE) 64 | SET (QT_USE_QTGUI TRUE) 65 | SET (QT_USE_QTSQL TRUE) 66 | SET (QT_USE_QTSVG TRUE) 67 | SET (QT_USE_QTXML TRUE) 68 | SET (QT_USE_QTNETWORK TRUE) 69 | FIND_PACKAGE(Qt4 REQUIRED) 70 | INCLUDE( ${QT_USE_FILE} ) 71 | ######################################################## 72 | # Build 73 | 74 | QT4_WRAP_UI (timtut2_UIS_H ${timtut2_UIS}) 75 | 76 | QT4_WRAP_CPP (timtut2_MOC_SRCS ${timtut2_MOC_HDRS}) 77 | 78 | QT4_ADD_RESOURCES(timtut2_RCC_SRCS ${timtut2_RCCS}) 79 | 80 | ADD_EXECUTABLE (timtut2 ${timtut2_SRCS} ${timtut2_MOC_SRCS} ${timtut2_RCC_SRCS} ${timtut2_UIS_H}) 81 | 82 | INCLUDE_DIRECTORIES( 83 | ${GDAL_INCLUDE_DIR} 84 | ${GEOS_INCLUDE_DIR} 85 | ${CMAKE_CURRENT_BINARY_DIR} 86 | ${QT_INCLUDE_DIR} 87 | ${QGIS_INCLUDE_DIR} 88 | . 89 | ) 90 | 91 | #This is probably no longer needed, but I will leave it in for Win machines for the moment 92 | IF(QT_QTSQL_FOUND) 93 | FIND_LIBRARY(QT_QTSQL_LIBRARY NAMES QtSql QtSql4 PATHS ${QT_LIBRARY_DIR} NO_DEFAULT_PATH) 94 | SET(QT_LIBRARIES ${QT_LIBRARIES} ${QT_QTSQL_LIBRARY}) 95 | ENDIF(QT_QTSQL_FOUND) 96 | 97 | TARGET_LINK_LIBRARIES(timtut2 98 | ${QT_LIBRARIES} 99 | ${QGIS_CORE_LIBRARY} 100 | ${QGIS_GUI_LIBRARY} 101 | ${GEOS_LIBRARY} 102 | ${GDAL_LIBRARY} 103 | ) 104 | 105 | IF (MSVC) 106 | # Very important or you get all kinds of odd side 107 | #effects like app crash on start up saying qtgui.dll 108 | TARGET_LINK_LIBRARIES( timtut2 109 | qtmain 110 | ) 111 | ENDIF (MSVC) 112 | 113 | ######################################################## 114 | # Install 115 | 116 | 117 | -------------------------------------------------------------------------------- /2_basic_main_window/README: -------------------------------------------------------------------------------- 1 | This tutorial is copyright Tim Sutton, 2006, 2008. 2 | 3 | This work is licensed under the GPL version 2 - see COPYING file for more details. 4 | -------------------------------------------------------------------------------- /2_basic_main_window/cmake_find_rules/FindGDAL.cmake: -------------------------------------------------------------------------------- 1 | ## Once run this will define: 2 | ## 3 | ## GDAL_FOUND = system has GDAL lib 4 | ## 5 | ## GDAL_LIBRARY = full path to the library 6 | ## 7 | ## GDAL_INCLUDE_DIR = where to find headers 8 | ## 9 | ## Magnus Homann 10 | 11 | 12 | IF(WIN32) 13 | 14 | IF (MINGW) 15 | FIND_PATH(GDAL_INCLUDE_DIR gdal.h /usr/local/include /usr/include c:/msys/local/include) 16 | FIND_LIBRARY(GDAL_LIBRARY NAMES gdal PATHS /usr/local/lib /usr/lib c:/msys/local/lib) 17 | ENDIF (MINGW) 18 | 19 | IF (MSVC) 20 | FIND_PATH(GDAL_INCLUDE_DIR gdal.h 21 | "$ENV{LIB_DIR}/include/gdal" 22 | ) 23 | FIND_LIBRARY(GDAL_LIBRARY NAMES gdal PATHS 24 | "$ENV{LIB_DIR}/lib/" 25 | ) 26 | 27 | # NOTE: under msvc you should add the following to your target link libraries 28 | # list as they are required by gdal 29 | #odbc32 odbccp32 30 | 31 | ENDIF (MSVC) 32 | 33 | 34 | ELSE(WIN32) 35 | IF(UNIX) 36 | 37 | # try to use framework on mac 38 | IF (APPLE) 39 | SET (GDAL_MAC_PATH /Library/Frameworks/GDAL.framework/unix/bin) 40 | ENDIF (APPLE) 41 | 42 | SET(GDAL_CONFIG_PREFER_PATH "$ENV{GDAL_HOME}/bin" CACHE STRING "preferred path to GDAL (gdal-config)") 43 | FIND_PROGRAM(GDAL_CONFIG gdal-config 44 | ${GDAL_CONFIG_PREFER_PATH} 45 | ${GDAL_MAC_PATH} 46 | /usr/local/bin/ 47 | /usr/bin/ 48 | ) 49 | # MESSAGE("DBG GDAL_CONFIG ${GDAL_CONFIG}") 50 | 51 | IF (GDAL_CONFIG) 52 | # set INCLUDE_DIR to prefix+include 53 | EXEC_PROGRAM(${GDAL_CONFIG} 54 | ARGS --prefix 55 | OUTPUT_VARIABLE GDAL_PREFIX) 56 | #SET(GDAL_INCLUDE_DIR ${GDAL_PREFIX}/include CACHE STRING INTERNAL) 57 | FIND_PATH(GDAL_INCLUDE_DIR 58 | gdal.h 59 | ${GDAL_PREFIX}/include/gdal 60 | ${GDAL_PREFIX}/include 61 | /usr/local/include 62 | /usr/include 63 | ) 64 | 65 | ## extract link dirs for rpath 66 | EXEC_PROGRAM(${GDAL_CONFIG} 67 | ARGS --libs 68 | OUTPUT_VARIABLE GDAL_CONFIG_LIBS ) 69 | 70 | ## split off the link dirs (for rpath) 71 | ## use regular expression to match wildcard equivalent "-L*" 72 | ## with is a space or a semicolon 73 | STRING(REGEX MATCHALL "[-][L]([^ ;])+" 74 | GDAL_LINK_DIRECTORIES_WITH_PREFIX 75 | "${GDAL_CONFIG_LIBS}" ) 76 | # MESSAGE("DBG GDAL_LINK_DIRECTORIES_WITH_PREFIX=${GDAL_LINK_DIRECTORIES_WITH_PREFIX}") 77 | 78 | ## remove prefix -L because we need the pure directory for LINK_DIRECTORIES 79 | 80 | IF (GDAL_LINK_DIRECTORIES_WITH_PREFIX) 81 | STRING(REGEX REPLACE "[-][L]" "" GDAL_LINK_DIRECTORIES ${GDAL_LINK_DIRECTORIES_WITH_PREFIX} ) 82 | ENDIF (GDAL_LINK_DIRECTORIES_WITH_PREFIX) 83 | 84 | ## split off the name 85 | ## use regular expression to match wildcard equivalent "-l*" 86 | ## with is a space or a semicolon 87 | STRING(REGEX MATCHALL "[-][l]([^ ;])+" 88 | GDAL_LIB_NAME_WITH_PREFIX 89 | "${GDAL_CONFIG_LIBS}" ) 90 | # MESSAGE("DBG GDAL_LIB_NAME_WITH_PREFIX=${GDAL_LIB_NAME_WITH_PREFIX}") 91 | 92 | 93 | ## remove prefix -l because we need the pure name 94 | 95 | IF (GDAL_LIB_NAME_WITH_PREFIX) 96 | STRING(REGEX REPLACE "[-][l]" "" GDAL_LIB_NAME ${GDAL_LIB_NAME_WITH_PREFIX} ) 97 | ENDIF (GDAL_LIB_NAME_WITH_PREFIX) 98 | 99 | IF (APPLE) 100 | SET(GDAL_LIBRARY ${GDAL_LINK_DIRECTORIES}/lib${GDAL_LIB_NAME}.dylib CACHE STRING INTERNAL) 101 | ELSE (APPLE) 102 | SET(GDAL_LIBRARY ${GDAL_LINK_DIRECTORIES}/lib${GDAL_LIB_NAME}.so CACHE STRING INTERNAL) 103 | ENDIF (APPLE) 104 | 105 | ELSE(GDAL_CONFIG) 106 | MESSAGE("FindGDAL.cmake: gdal-config not found. Please set it manually. GDAL_CONFIG=${GDAL_CONFIG}") 107 | ENDIF(GDAL_CONFIG) 108 | 109 | ENDIF(UNIX) 110 | ENDIF(WIN32) 111 | 112 | 113 | IF (GDAL_INCLUDE_DIR AND GDAL_LIBRARY) 114 | SET(GDAL_FOUND TRUE) 115 | ENDIF (GDAL_INCLUDE_DIR AND GDAL_LIBRARY) 116 | 117 | IF (GDAL_FOUND) 118 | 119 | IF (NOT GDAL_FIND_QUIETLY) 120 | MESSAGE(STATUS "Found GDAL: ${GDAL_LIBRARY}") 121 | ENDIF (NOT GDAL_FIND_QUIETLY) 122 | 123 | ELSE (GDAL_FOUND) 124 | 125 | MESSAGE(GDAL_INCLUDE_DIR=${GDAL_INCLUDE_DIR}) 126 | MESSAGE(GDAL_LIBRARY=${GDAL_LIBRARY}) 127 | MESSAGE(FATAL_ERROR "Could not find GDAL") 128 | 129 | ENDIF (GDAL_FOUND) 130 | -------------------------------------------------------------------------------- /2_basic_main_window/cmake_find_rules/FindGEOS.cmake: -------------------------------------------------------------------------------- 1 | 2 | # CMake module to search for GEOS library 3 | # 4 | # If it's found it sets GEOS_FOUND to TRUE 5 | # and following variables are set: 6 | # GEOS_INCLUDE_DIR 7 | # GEOS_LIBRARY 8 | 9 | 10 | FIND_PATH(GEOS_INCLUDE_DIR geos.h 11 | /usr/local/include 12 | /usr/include 13 | #MSVC 14 | "$ENV{LIB_DIR}/include" 15 | #mingw 16 | c:/msys/local/include 17 | ) 18 | 19 | FIND_LIBRARY(GEOS_LIBRARY NAMES geos PATHS 20 | /usr/local/lib 21 | /usr/lib 22 | #MSVC 23 | "$ENV{LIB_DIR}/lib" 24 | #mingw 25 | c:/msys/local/lib 26 | ) 27 | 28 | IF (GEOS_INCLUDE_DIR AND GEOS_LIBRARY) 29 | SET(GEOS_FOUND TRUE) 30 | ENDIF (GEOS_INCLUDE_DIR AND GEOS_LIBRARY) 31 | 32 | 33 | IF (GEOS_FOUND) 34 | 35 | IF (NOT GEOS_FIND_QUIETLY) 36 | MESSAGE(STATUS "Found GEOS: ${GEOS_LIBRARY}") 37 | ENDIF (NOT GEOS_FIND_QUIETLY) 38 | 39 | ELSE (GEOS_FOUND) 40 | 41 | IF (GEOS_FIND_REQUIRED) 42 | MESSAGE(FATAL_ERROR "Could not find GEOS") 43 | ENDIF (GEOS_FIND_REQUIRED) 44 | 45 | ENDIF (GEOS_FOUND) 46 | -------------------------------------------------------------------------------- /2_basic_main_window/cmake_find_rules/FindQGIS.cmake: -------------------------------------------------------------------------------- 1 | ## Once run this will define: 2 | ## 3 | ## QGIS_FOUND = system has QGIS lib 4 | ## 5 | ## QGIS_CORE_LIBRARY = full path to the CORE library 6 | ## QGIS_GUI_LIBRARY = full path to the GUI library 7 | ## QGIS_PLUGIN_DIR = full path to where QGIS plugins are installed 8 | ## QGIS_INCLUDE_DIR = where to find headers 9 | ## 10 | ## Tim Sutton 11 | 12 | #MESSAGE("Searching for QGIS") 13 | IF(WIN32) 14 | #MESSAGE("Searching for QGIS in C:/program files/Quantum GIS") 15 | IF (MINGW) 16 | FIND_PATH(QGIS_PLUGIN_DIR libnortharrowplugin.dll 17 | "C:/Program Files/Quantum GIS/plugins" 18 | ) 19 | FIND_PATH(QGIS_INCLUDE_DIR qgsapplication.h 20 | "C:/Program Files/Quantum GIS/include" 21 | ) 22 | FIND_LIBRARY(QGIS_CORE_LIBRARY NAMES qgis_core PATHS 23 | "C:/Program Files/Quantum GIS/" 24 | ) 25 | FIND_LIBRARY(QGIS_GUI_LIBRARY NAMES qgis_gui PATHS 26 | "C:/Program Files/Quantum GIS/" 27 | ) 28 | ENDIF (MINGW) 29 | 30 | IF (MSVC) 31 | FIND_PATH(QGIS_PLUGIN_DIR libnortharrowplugin.dll 32 | "C:/Program Files/Quantum GIS/lib/qgis" 33 | ) 34 | FIND_PATH(QGIS_INCLUDE_DIR qgsapplication.h 35 | "$ENV{LIB_DIR}/include/qgis" 36 | ) 37 | FIND_LIBRARY(QGIS_CORE_LIBRARY NAMES qgis_core PATHS 38 | "$ENV{LIB_DIR}/lib/" 39 | ) 40 | FIND_LIBRARY(QGIS_GUI_LIBRARY NAMES qgis_gui PATHS 41 | "$ENV{LIB_DIR}/lib/" 42 | ) 43 | ENDIF (MSVC) 44 | 45 | ELSE(WIN32) 46 | IF(UNIX) 47 | 48 | # try to use bundle on mac 49 | IF (APPLE) 50 | #MESSAGE("Searching for QGIS in /Applications/QGIS.app/Contents/MacOS") 51 | SET (QGIS_MAC_PATH /Applications/QGIS1.0.0.app/Contents/MacOS) 52 | SET (QGIS_LIB_DIR ${QGIS_MAC_PATH}/lib) 53 | SET (QGIS_PLUGIN_DIR ${QGIS_MAC_PATH}/lib/qgis CACHE STRING INTERNAL) 54 | # set INCLUDE_DIR to prefix+include 55 | SET(QGIS_INCLUDE_DIR ${QGIS_MAC_PATH}/include/qgis CACHE STRING INTERNAL) 56 | ## extract link dirs 57 | SET(QGIS_CORE_LIBRARY ${QGIS_LIB_DIR}/libqgis_core.dylib CACHE STRING INTERNAL) 58 | SET(QGIS_GUI_LIBRARY ${QGIS_LIB_DIR}/libqgis_gui.dylib CACHE STRING INTERNAL) 59 | ELSE (APPLE) # Unix / Linux 60 | #MESSAGE("Searching for QGIS in /usr/bin; /usr/local/bin") 61 | FIND_PATH(QGIS_PLUGIN_DIR libnortharrowplugin.so 62 | /usr/lib/qgis 63 | /usr/local/lib/qgis 64 | "$ENV{LIB_DIR}/lib/qgis" 65 | ) 66 | FIND_PATH(QGIS_INCLUDE_DIR qgis.h 67 | /usr/include/qgis 68 | /usr/local/include/qgis 69 | "$ENV{LIB_DIR}/include/qgis" 70 | ) 71 | FIND_LIBRARY(QGIS_CORE_LIBRARY NAMES qgis_core PATHS 72 | /usr/lib 73 | /usr/local/lib 74 | "$ENV{LIB_DIR}/lib" 75 | ) 76 | FIND_LIBRARY(QGIS_GUI_LIBRARY NAMES qgis_gui PATHS 77 | /usr/lib 78 | /usr/local/lib 79 | "$ENV{LIB_DIR}/lib" 80 | ) 81 | ENDIF (APPLE) 82 | ENDIF(UNIX) 83 | ENDIF(WIN32) 84 | 85 | 86 | IF (QGIS_INCLUDE_DIR AND QGIS_CORE_LIBRARY AND QGIS_GUI_LIBRARY) 87 | SET(QGIS_FOUND TRUE) 88 | ENDIF (QGIS_INCLUDE_DIR AND QGIS_CORE_LIBRARY AND QGIS_GUI_LIBRARY) 89 | 90 | IF (QGIS_FOUND) 91 | IF (NOT QGIS_FIND_QUIETLY) 92 | MESSAGE(STATUS "Found QGIS Core: ${QGIS_CORE_LIBRARY}") 93 | MESSAGE(STATUS "Found QGIS Gui: ${QGIS_GUI_LIBRARY}") 94 | MESSAGE(STATUS "Found QGIS Plugins Dir: ${QGIS_PLUGIN_DIR}") 95 | ENDIF (NOT QGIS_FIND_QUIETLY) 96 | ELSE (QGIS_FOUND) 97 | IF (QGIS_FIND_REQUIRED) 98 | MESSAGE(FATAL_ERROR "Could not find QGIS") 99 | ENDIF (QGIS_FIND_REQUIRED) 100 | ENDIF (QGIS_FOUND) 101 | -------------------------------------------------------------------------------- /2_basic_main_window/data/test.dbf: -------------------------------------------------------------------------------- 1 | _AQNameCP Bob QGIS Rocks Tim Pumpkin Aru Savledore Happy Happy -------------------------------------------------------------------------------- /2_basic_main_window/data/test.prj: -------------------------------------------------------------------------------- 1 | GEOGCS["GCS_WGS_1984",DATUM["D_unknown",SPHEROID["WGS84",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]] -------------------------------------------------------------------------------- /2_basic_main_window/data/test.shp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/2_basic_main_window/data/test.shp -------------------------------------------------------------------------------- /2_basic_main_window/data/test.shx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/2_basic_main_window/data/test.shx -------------------------------------------------------------------------------- /2_basic_main_window/images/tutorial2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/2_basic_main_window/images/tutorial2.jpg -------------------------------------------------------------------------------- /2_basic_main_window/mActionAddLayer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/2_basic_main_window/mActionAddLayer.png -------------------------------------------------------------------------------- /2_basic_main_window/mActionPan.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/2_basic_main_window/mActionPan.png -------------------------------------------------------------------------------- /2_basic_main_window/mActionZoomIn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/2_basic_main_window/mActionZoomIn.png -------------------------------------------------------------------------------- /2_basic_main_window/mActionZoomOut.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/2_basic_main_window/mActionZoomOut.png -------------------------------------------------------------------------------- /2_basic_main_window/main.cpp: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * Copyright (C) 2006 by Tim Sutton * 3 | * tim@linfiniti.com * 4 | * * 5 | * This program is free software; you can redistribute it and/or modify * 6 | * it under the terms of the GNU General Public License as published by * 7 | * the Free Software Foundation; either version 2 of the License, or * 8 | * (at your option) any later version. * 9 | * * 10 | * This program is distributed in the hope that it will be useful, * 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 13 | * GNU General Public License for more details. * 14 | * * 15 | * You should have received a copy of the GNU General Public License * 16 | * along with this program; if not, write to the * 17 | * Free Software Foundation, Inc., * 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 19 | ***************************************************************************/ 20 | // 21 | // Qt Includes 22 | // 23 | #include 24 | #include 25 | // 26 | // QGIS Includes 27 | // 28 | #include 29 | 30 | int main(int argc, char ** argv) 31 | { 32 | // Start the Application 33 | QgsApplication app(argc, argv, TRUE); 34 | MainWindow * mypMainWindow = new MainWindow(); 35 | mypMainWindow->show(); 36 | // Start the Application Event Loop 37 | return app.exec(); 38 | } 39 | -------------------------------------------------------------------------------- /2_basic_main_window/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * Copyright (C) 2006 by Tim Sutton * 3 | * tim@linfiniti.com * 4 | * * 5 | * This program is free software; you can redistribute it and/or modify * 6 | * it under the terms of the GNU General Public License as published by * 7 | * the Free Software Foundation; either version 2 of the License, or * 8 | * (at your option) any later version. * 9 | * * 10 | * This program is distributed in the hope that it will be useful, * 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 13 | * GNU General Public License for more details. * 14 | * * 15 | * You should have received a copy of the GNU General Public License * 16 | * along with this program; if not, write to the * 17 | * Free Software Foundation, Inc., * 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 19 | ***************************************************************************/ 20 | #include "mainwindow.h" 21 | // 22 | // QGIS Includes 23 | // 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | // 31 | // QGIS Map tools 32 | // 33 | #include "qgsmaptoolpan.h" 34 | #include "qgsmaptoolzoom.h" 35 | // 36 | // These are the other headers for available map tools (not used in this example) 37 | // 38 | //#include "qgsmaptoolcapture.h" 39 | //#include "qgsmaptoolidentify.h" 40 | //#include "qgsmaptoolselect.h" 41 | //#include "qgsmaptoolvertexedit.h" 42 | //#include "qgsmeasure.h" 43 | // 44 | 45 | MainWindow::MainWindow(QWidget* parent, Qt::WFlags fl) 46 | : QMainWindow(parent,fl) 47 | { 48 | //required by Qt4 to initialise the ui 49 | setupUi(this); 50 | 51 | // Instantiate Provider Registry 52 | #if defined(Q_WS_MAC) 53 | // 54 | // Be sure to set this to an appropriate place if you are on OSX 55 | // 56 | QString myPluginsDir = "/Users/timsutton/apps/qgis.app/Contents/MacOS/lib/qgis"; 57 | #else 58 | // 59 | // Be sure to set this to an appropriate place if you are on Linux or windows 60 | // 61 | QString myPluginsDir = "/home/timlinux/apps/qgis_trunk/lib/qgis"; 62 | #endif 63 | QgsProviderRegistry::instance(myPluginsDir); 64 | 65 | 66 | // Create the Map Canvas 67 | mpMapCanvas= new QgsMapCanvas(0, 0); 68 | mpMapCanvas->enableAntiAliasing(true); 69 | mpMapCanvas->useImageToRender(false); 70 | mpMapCanvas->setCanvasColor(QColor(255, 255, 255)); 71 | mpMapCanvas->freeze(false); 72 | mpMapCanvas->setVisible(true); 73 | mpMapCanvas->refresh(); 74 | mpMapCanvas->show(); 75 | 76 | // Lay our widgets out in the main window 77 | mpLayout = new QVBoxLayout(frameMap); 78 | mpLayout->addWidget(mpMapCanvas); 79 | 80 | //create the action behaviours 81 | connect(mpActionPan, SIGNAL(triggered()), this, SLOT(panMode())); 82 | connect(mpActionZoomIn, SIGNAL(triggered()), this, SLOT(zoomInMode())); 83 | connect(mpActionZoomOut, SIGNAL(triggered()), this, SLOT(zoomOutMode())); 84 | connect(mpActionAddLayer, SIGNAL(triggered()), this, SLOT(addLayer())); 85 | 86 | //create a little toolbar 87 | mpMapToolBar = addToolBar(tr("File")); 88 | mpMapToolBar->addAction(mpActionAddLayer); 89 | mpMapToolBar->addAction(mpActionZoomIn); 90 | mpMapToolBar->addAction(mpActionZoomOut); 91 | mpMapToolBar->addAction(mpActionPan); 92 | 93 | //create the maptools 94 | mpPanTool = new QgsMapToolPan(mpMapCanvas); 95 | mpPanTool->setAction(mpActionPan); 96 | mpZoomInTool = new QgsMapToolZoom(mpMapCanvas, FALSE); // false = in 97 | mpZoomInTool->setAction(mpActionZoomIn); 98 | mpZoomOutTool = new QgsMapToolZoom(mpMapCanvas, TRUE ); //true = out 99 | mpZoomOutTool->setAction(mpActionZoomOut); 100 | } 101 | 102 | MainWindow::~MainWindow() 103 | { 104 | delete mpZoomInTool; 105 | delete mpZoomOutTool; 106 | delete mpPanTool; 107 | delete mpMapToolBar; 108 | delete mpMapCanvas; 109 | delete mpLayout; 110 | } 111 | 112 | void MainWindow::panMode() 113 | { 114 | mpMapCanvas->setMapTool(mpPanTool); 115 | 116 | } 117 | void MainWindow::zoomInMode() 118 | { 119 | mpMapCanvas->setMapTool(mpZoomInTool); 120 | } 121 | void MainWindow::zoomOutMode() 122 | { 123 | mpMapCanvas->setMapTool(mpZoomOutTool); 124 | } 125 | void MainWindow::addLayer() 126 | { 127 | QString myLayerPath = "../data"; 128 | QString myLayerBaseName = "test"; 129 | QString myProviderName = "ogr"; 130 | 131 | QgsVectorLayer * mypLayer = new QgsVectorLayer(myLayerPath, myLayerBaseName, myProviderName); 132 | QgsSingleSymbolRenderer *mypRenderer = new QgsSingleSymbolRenderer(mypLayer->geometryType()); 133 | QList myLayerSet; 134 | mypLayer->setRenderer(mypRenderer); 135 | 136 | if (mypLayer->isValid()) 137 | { 138 | qDebug("Layer is valid"); 139 | } 140 | else 141 | { 142 | qDebug("Layer is NOT valid"); 143 | return; 144 | } 145 | // Add the Vector Layer to the Layer Registry 146 | QgsMapLayerRegistry::instance()->addMapLayer(mypLayer, TRUE); 147 | 148 | // Add the Layer to the Layer Set 149 | myLayerSet.append(QgsMapCanvasLayer(mypLayer)); 150 | // set the canvas to the extent of our layer 151 | mpMapCanvas->setExtent(mypLayer->extent()); 152 | // Set the Map Canvas Layer Set 153 | mpMapCanvas->setLayerSet(myLayerSet); 154 | } 155 | 156 | -------------------------------------------------------------------------------- /2_basic_main_window/mainwindow.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * Copyright (C) 2006 by Tim Sutton * 3 | * tim@linfiniti.com * 4 | * * 5 | * This program is free software; you can redistribute it and/or modify * 6 | * it under the terms of the GNU General Public License as published by * 7 | * the Free Software Foundation; either version 2 of the License, or * 8 | * (at your option) any later version. * 9 | * * 10 | * This program is distributed in the hope that it will be useful, * 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 13 | * GNU General Public License for more details. * 14 | * * 15 | * You should have received a copy of the GNU General Public License * 16 | * along with this program; if not, write to the * 17 | * Free Software Foundation, Inc., * 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 19 | ***************************************************************************/ 20 | #ifndef OMGMAINWINDOW_H 21 | #define OMGMAINWINDOW_H 22 | 23 | //QGis Includes 24 | #include 25 | #include 26 | 27 | //QT Includes 28 | #include 29 | 30 | //Local Includes 31 | #include 32 | 33 | /** 34 | @author Tim Sutton 35 | */ 36 | class MainWindow : public QMainWindow, private Ui::MainWindowBase 37 | { 38 | Q_OBJECT; 39 | public: 40 | MainWindow(QWidget* parent = 0, Qt::WFlags fl = 0 ); 41 | ~MainWindow(); 42 | public slots: 43 | void zoomInMode(); 44 | void zoomOutMode(); 45 | void panMode(); 46 | void addLayer(); 47 | 48 | private: 49 | QgsMapCanvas * mpMapCanvas; 50 | QVBoxLayout * mpLayout; 51 | QToolBar * mpMapToolBar; 52 | QgsMapTool * mpPanTool; 53 | QgsMapTool * mpZoomInTool; 54 | QgsMapTool * mpZoomOutTool; 55 | 56 | }; 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /2_basic_main_window/mainwindowbase.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | MainWindowBase 6 | 7 | 8 | 9 | 0 10 | 0 11 | 579 12 | 330 13 | 14 | 15 | 16 | MainWindow 17 | 18 | 19 | 20 | 21 | 9 22 | 23 | 24 | 6 25 | 26 | 27 | 28 | 29 | QFrame::StyledPanel 30 | 31 | 32 | QFrame::Raised 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 0 42 | 0 43 | 579 44 | 22 45 | 46 | 47 | 48 | 49 | Map 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | :/mActionZoomIn.png 62 | 63 | 64 | Zoom In 65 | 66 | 67 | 68 | 69 | :/mActionZoomOut.png 70 | 71 | 72 | Zoom Out 73 | 74 | 75 | 76 | 77 | :/mActionPan.png 78 | 79 | 80 | Pan 81 | 82 | 83 | 84 | 85 | :/mActionAddLayer.png 86 | 87 | 88 | Add Layer 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | -------------------------------------------------------------------------------- /2_basic_main_window/resources.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | mActionAddLayer.png 4 | mActionPan.png 5 | mActionZoomIn.png 6 | mActionZoomOut.png 7 | 8 | 9 | -------------------------------------------------------------------------------- /2_basic_main_window/tutorial2.t2t: -------------------------------------------------------------------------------- 1 | Tutorial 2 - Working with QgsMapCanvas 2 | 3 | Tim Sutton, 2008 4 | %! target : html 5 | %! style : style.css 6 | %! Options : --toc --toc-level 3 --enum-title --css-sugar --css-inside 7 | %! preproc : TUT_URL https://qgis.org 8 | %! PostProc(html): '(?i)(```)' '
\1' 9 | %! PostProc(html): '(?i)(```)' '\1
' 10 | %! encoding: iso-8859-1 11 | % These are comments and will not be generated in any output 12 | % ------------------- 13 | %This document is in text2tags format. You can generate html, plain text and 14 | %moinmoin formatted documentation by running txt2tags on this document. See the 15 | %txt2tags home page for more details. Please insert manual line breaks in this 16 | %document as it makes diffing for changes much easier. To do this in vim 17 | %automatically, select a section then issue (gq) command. Please dont 18 | %apply vim formatting to the whol document as it screws up some formatting 19 | %rather apply it selectively to paragraphs where needed. 20 | % To generate the text version of this document: 21 | % 22 | % Make sure you are in the top level code examples dir 23 | % since image paths are referenced from there: 24 | % 25 | % txt2tags -t txt -o tutorial2 2_basic_main_window/tutorial2.t2t 26 | % To generate the moinmoin version of this document 27 | % txt2tags -t moin -o tutorial2.moin 2_basic_main_window/tutorial2.t2t 28 | % To generate the html version of this document 29 | % txt2tags -t html -o tutorial2.html 2_basic_main_window/tutorial2.t2t 30 | % End of comments 31 | % ------------------- 32 | 33 | = Working with QgsMapCanvas = 34 | 35 | [images/tim50x50.png]Tutorial 1 showed you the useage of the QgsMapCanvas api 36 | to create a simple application that loads a shapefile and displays the points 37 | in it. But what good is map that you can't interact with? Enter our second 38 | tutorial where we show you how to use QgsMapTool - the base class for all tools 39 | that need to interact with the canvas. 40 | 41 | **Updated November 2008 to use cmake build system and the updated QGIS 1.0.0 42 | API. Tim** 43 | 44 | [images/tutorial2.jpg] 45 | 46 | Today I will extend the last tutorial by making it a QMainWindow application 47 | with a menu, toolbar and canvas area. The purpose of the tutorial is to provide 48 | a demonstrator project, so I wont promise to write the most elegant or robust 49 | C++ code. The project will provide 4 toolbar icons for 50 | 51 | 52 | - loading a map layer (layer name is hard coded in the application 53 | - zooming in 54 | - zooming out 55 | - panning 56 | - 57 | 58 | The entire project can be checked out form the QGIS Subversion repository using 59 | the following command: 60 | 61 | ```svn co https://svn.osgeo.org/qgis/trunk/code_examples/2_basic_main_window``` 62 | 63 | 64 | In the working directory for the tutorial code you will find a number of files 65 | including c++ sources, icons and a simple data file under data. There is also 66 | the .ui file for the main window. 67 | 68 | Since much of the code is the same as the previous tutorial, I will focus on 69 | the MapTool specifics - the rest of the implementation details can be 70 | investigated by checking out the project form SVN. A QgsMapTool is a class that 71 | interacts with the MapCanvas using the mouse pointer. QGIS has a number of 72 | QgsMapTools implemented, and you can subclass QgsMapTool to create your own. In 73 | mainwindow.cpp you will see I include the headers for the QgsMapTools near the 74 | start of the file: 75 | 76 | ``` 77 | // 78 | // QGIS Map tools 79 | // 80 | #include "qgsmaptoolpan.h" 81 | #include "qgsmaptoolzoom.h" 82 | // 83 | // These are the other headers for available map tools 84 | // (not used in this example) 85 | // 86 | //#include "qgsmaptoolcapture.h" 87 | //#include "qgsmaptoolidentify.h" 88 | //#include "qgsmaptoolselect.h" 89 | //#include "qgsmaptoolvertexedit.h" 90 | //#include "qgsmeasure.h" 91 | ``` 92 | 93 | As you can see, I am only using two types of MapTool subclasses for this 94 | tutorial, but there are more available in the QGIS library. Hooking up our 95 | MapTools to the canvas is very easy using the normal Qt4 signal/slot mechanism: 96 | 97 | ``` 98 | //create the action behaviours 99 | connect(mActionPan, SIGNAL(triggered()), this, SLOT(panMode())); 100 | connect(mActionZoomIn, SIGNAL(triggered()), this, SLOT(zoomInMode())); 101 | connect(mActionZoomOut, SIGNAL(triggered()), this, SLOT(zoomOutMode())); 102 | connect(mActionAddLayer, SIGNAL(triggered()), this, SLOT(addLayer())); 103 | ``` 104 | 105 | Next we make a small toolbar to hold our toolbuttons. Note that the mpAction* 106 | actions were created in designer. 107 | 108 | ``` 109 | //create a little toolbar 110 | mpMapToolBar = addToolBar(tr("File")); 111 | mpMapToolBar->addAction(mpActionAddLayer); 112 | mpMapToolBar->addAction(mpActionZoomIn); 113 | mpMapToolBar->addAction(mpActionZoomOut); 114 | mpMapToolBar->addAction(mpActionPan); 115 | ``` 116 | 117 | Thats really pretty straightforward Qt stuff too. Now we create our three map 118 | tools: 119 | 120 | ``` 121 | //create the maptools 122 | mpPanTool = new QgsMapToolPan(mpMapCanvas); 123 | mpPanTool->setAction(mpActionPan); 124 | mpZoomInTool = new QgsMapToolZoom(mpMapCanvas, FALSE); // false = in 125 | mpZoomInTool->setAction(mpActionZoomIn); 126 | mpZoomOutTool = new QgsMapToolZoom(mpMapCanvas, TRUE ); //true = out 127 | mpZoomOutTool->setAction(mpActionZoomOut); 128 | ``` 129 | 130 | Again nothing here is very complicated - we are creating tool instances, each 131 | of which is associated with the same mapcanvas, and a different QAction. When 132 | the user selects one of the toolbar icons, the active MapTool for the canvas is 133 | set. For example when the pan icon is clicked, we do this: 134 | 135 | ``` 136 | void MainWindow::panMode() 137 | { 138 | mpMapCanvas->setMapTool(mpPanTool); 139 | } 140 | ``` 141 | 142 | 143 | **Conclusion** 144 | 145 | As you can see extending our previous example into something more functional 146 | using MapTools is really easy and only requires a few lines of code for each 147 | MapTool you want to provide. 148 | 149 | You can check out and build this tutorial using SVN and CMake using the 150 | following steps: 151 | 152 | ``` 153 | svn co https://svn.osgeo.org/qgis/trunk/code_examples/2_basic_main_window 154 | cd 2_basic_main_window 155 | mkdir build 156 | cd build 157 | #optionally specify where your QGIS is installed (should work on all platforms) 158 | #if your QGIS is installed to /usr or /usr/local you can leave this next step out 159 | export LIB_DIR=/home/timlinux/apps 160 | cmake .. 161 | make 162 | ./timtut2 163 | ``` 164 | 165 | **A final note** 166 | 167 | I don't often check the comments on these posts - if you are stuck please 168 | write to the [QGIS Developer mailing list 169 | http://qgis.org/content/view/115/96/"]! Thanks.Tim 170 | 171 | -------------------------------------------------------------------------------- /3_basic_labelling/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.6) 2 | PROJECT(timtut3) 3 | SET(CMAKE_COLOR_MAKEFILE ON) 4 | # set path to additional CMake modules 5 | SET(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake_find_rules ${CMAKE_MODULE_PATH}) 6 | FIND_PACKAGE(QGIS REQUIRED) 7 | IF (NOT QGIS_FOUND) 8 | MESSAGE (SEND_ERROR "QGIS dependency was not found!") 9 | ENDIF (NOT QGIS_FOUND) 10 | IF (WIN32) 11 | # expect that classes are being imported by default 12 | # Note: MSVC doesn't like when the macros are quotes 13 | # and MSYS doesn't like them unqouted (bacause of braces) 14 | # import qgis classes 15 | IF (MSVC) 16 | ADD_DEFINITIONS("-DGUI_EXPORT=__declspec(dllimport)") 17 | ADD_DEFINITIONS("-DCORE_EXPORT=__declspec(dllimport)") 18 | ELSE (MSVC) 19 | ADD_DEFINITIONS("\"-DGUI_EXPORT=__declspec(dllimport)\"") 20 | ADD_DEFINITIONS("\"-DCORE_EXPORT=__declspec(dllimport)\"") 21 | ENDIF (MSVC) 22 | ELSE (WIN32) 23 | ADD_DEFINITIONS(-DGUI_EXPORT=) 24 | ADD_DEFINITIONS(-DCORE_EXPORT=) 25 | ENDIF (WIN32) 26 | 27 | FIND_PACKAGE(GDAL REQUIRED) 28 | IF (NOT GDAL_FOUND) 29 | MESSAGE (SEND_ERROR "GDAL dependency was not found!") 30 | ENDIF (NOT GDAL_FOUND) 31 | 32 | FIND_PACKAGE(GEOS REQUIRED) 33 | IF (NOT GEOS_FOUND) 34 | MESSAGE (SEND_ERROR "GEOS dependency was not found!") 35 | ENDIF (NOT GEOS_FOUND) 36 | 37 | IF (CMAKE_BUILD_TYPE MATCHES Debug) 38 | ADD_DEFINITIONS(-Dtimtut3DEBUG=1) 39 | ENDIF (CMAKE_BUILD_TYPE MATCHES Debug) 40 | ######################################################## 41 | # Files 42 | 43 | SET (timtut3_SRCS 44 | main.cpp 45 | mainwindow.cpp 46 | ) 47 | 48 | # This tut uses no UIs 49 | SET (timtut3_UIS 50 | mainwindowbase.ui 51 | ) 52 | 53 | # This tut needs no MOC 54 | SET (timtut3_MOC_HDRS 55 | mainwindow.h 56 | ) 57 | 58 | 59 | SET (timtut3_RCCS 60 | resources.qrc 61 | ) 62 | 63 | SET (QT_USE_QT3SUPPORT FALSE) 64 | SET (QT_USE_QTGUI TRUE) 65 | SET (QT_USE_QTSQL TRUE) 66 | SET (QT_USE_QTSVG TRUE) 67 | SET (QT_USE_QTXML TRUE) 68 | SET (QT_USE_QTNETWORK TRUE) 69 | FIND_PACKAGE(Qt4 REQUIRED) 70 | INCLUDE( ${QT_USE_FILE} ) 71 | ######################################################## 72 | # Build 73 | 74 | QT4_WRAP_UI (timtut3_UIS_H ${timtut3_UIS}) 75 | 76 | QT4_WRAP_CPP (timtut3_MOC_SRCS ${timtut3_MOC_HDRS}) 77 | 78 | QT4_ADD_RESOURCES(timtut3_RCC_SRCS ${timtut3_RCCS}) 79 | 80 | ADD_EXECUTABLE (timtut3 ${timtut3_SRCS} ${timtut3_MOC_SRCS} ${timtut3_RCC_SRCS} ${timtut3_UIS_H}) 81 | 82 | INCLUDE_DIRECTORIES( 83 | ${GDAL_INCLUDE_DIR} 84 | ${GEOS_INCLUDE_DIR} 85 | ${CMAKE_CURRENT_BINARY_DIR} 86 | ${QT_INCLUDE_DIR} 87 | ${QGIS_INCLUDE_DIR} 88 | . 89 | ) 90 | 91 | #This is probably no longer needed, but I will leave it in for Win machines for the moment 92 | IF(QT_QTSQL_FOUND) 93 | FIND_LIBRARY(QT_QTSQL_LIBRARY NAMES QtSql QtSql4 PATHS ${QT_LIBRARY_DIR} NO_DEFAULT_PATH) 94 | SET(QT_LIBRARIES ${QT_LIBRARIES} ${QT_QTSQL_LIBRARY}) 95 | ENDIF(QT_QTSQL_FOUND) 96 | 97 | TARGET_LINK_LIBRARIES(timtut3 98 | ${QT_LIBRARIES} 99 | ${QGIS_CORE_LIBRARY} 100 | ${QGIS_GUI_LIBRARY} 101 | ${GEOS_LIBRARY} 102 | ${GDAL_LIBRARY} 103 | ) 104 | 105 | IF (MSVC) 106 | # Very important or you get all kinds of odd side 107 | #effects like app crash on start up saying qtgui.dll 108 | TARGET_LINK_LIBRARIES( timtut3 109 | qtmain 110 | ) 111 | ENDIF (MSVC) 112 | 113 | ######################################################## 114 | # Install 115 | 116 | 117 | -------------------------------------------------------------------------------- /3_basic_labelling/README: -------------------------------------------------------------------------------- 1 | This tutorial is copyright Tim Sutton, 2006, 2008. 2 | 3 | This work is licensed under the GPL version 2 - see COPYING file for more details. 4 | -------------------------------------------------------------------------------- /3_basic_labelling/cmake_find_rules/FindGDAL.cmake: -------------------------------------------------------------------------------- 1 | ## Once run this will define: 2 | ## 3 | ## GDAL_FOUND = system has GDAL lib 4 | ## 5 | ## GDAL_LIBRARY = full path to the library 6 | ## 7 | ## GDAL_INCLUDE_DIR = where to find headers 8 | ## 9 | ## Magnus Homann 10 | 11 | 12 | IF(WIN32) 13 | 14 | IF (MINGW) 15 | FIND_PATH(GDAL_INCLUDE_DIR gdal.h /usr/local/include /usr/include c:/msys/local/include) 16 | FIND_LIBRARY(GDAL_LIBRARY NAMES gdal PATHS /usr/local/lib /usr/lib c:/msys/local/lib) 17 | ENDIF (MINGW) 18 | 19 | IF (MSVC) 20 | FIND_PATH(GDAL_INCLUDE_DIR gdal.h 21 | "$ENV{LIB_DIR}/include/gdal" 22 | ) 23 | FIND_LIBRARY(GDAL_LIBRARY NAMES gdal PATHS 24 | "$ENV{LIB_DIR}/lib/" 25 | ) 26 | 27 | # NOTE: under msvc you should add the following to your target link libraries 28 | # list as they are required by gdal 29 | #odbc32 odbccp32 30 | 31 | ENDIF (MSVC) 32 | 33 | 34 | ELSE(WIN32) 35 | IF(UNIX) 36 | 37 | # try to use framework on mac 38 | IF (APPLE) 39 | SET (GDAL_MAC_PATH /Library/Frameworks/GDAL.framework/unix/bin) 40 | ENDIF (APPLE) 41 | 42 | SET(GDAL_CONFIG_PREFER_PATH "$ENV{GDAL_HOME}/bin" CACHE STRING "preferred path to GDAL (gdal-config)") 43 | FIND_PROGRAM(GDAL_CONFIG gdal-config 44 | ${GDAL_CONFIG_PREFER_PATH} 45 | ${GDAL_MAC_PATH} 46 | /usr/local/bin/ 47 | /usr/bin/ 48 | ) 49 | # MESSAGE("DBG GDAL_CONFIG ${GDAL_CONFIG}") 50 | 51 | IF (GDAL_CONFIG) 52 | # set INCLUDE_DIR to prefix+include 53 | EXEC_PROGRAM(${GDAL_CONFIG} 54 | ARGS --prefix 55 | OUTPUT_VARIABLE GDAL_PREFIX) 56 | #SET(GDAL_INCLUDE_DIR ${GDAL_PREFIX}/include CACHE STRING INTERNAL) 57 | FIND_PATH(GDAL_INCLUDE_DIR 58 | gdal.h 59 | ${GDAL_PREFIX}/include/gdal 60 | ${GDAL_PREFIX}/include 61 | /usr/local/include 62 | /usr/include 63 | ) 64 | 65 | ## extract link dirs for rpath 66 | EXEC_PROGRAM(${GDAL_CONFIG} 67 | ARGS --libs 68 | OUTPUT_VARIABLE GDAL_CONFIG_LIBS ) 69 | 70 | ## split off the link dirs (for rpath) 71 | ## use regular expression to match wildcard equivalent "-L*" 72 | ## with is a space or a semicolon 73 | STRING(REGEX MATCHALL "[-][L]([^ ;])+" 74 | GDAL_LINK_DIRECTORIES_WITH_PREFIX 75 | "${GDAL_CONFIG_LIBS}" ) 76 | # MESSAGE("DBG GDAL_LINK_DIRECTORIES_WITH_PREFIX=${GDAL_LINK_DIRECTORIES_WITH_PREFIX}") 77 | 78 | ## remove prefix -L because we need the pure directory for LINK_DIRECTORIES 79 | 80 | IF (GDAL_LINK_DIRECTORIES_WITH_PREFIX) 81 | STRING(REGEX REPLACE "[-][L]" "" GDAL_LINK_DIRECTORIES ${GDAL_LINK_DIRECTORIES_WITH_PREFIX} ) 82 | ENDIF (GDAL_LINK_DIRECTORIES_WITH_PREFIX) 83 | 84 | ## split off the name 85 | ## use regular expression to match wildcard equivalent "-l*" 86 | ## with is a space or a semicolon 87 | STRING(REGEX MATCHALL "[-][l]([^ ;])+" 88 | GDAL_LIB_NAME_WITH_PREFIX 89 | "${GDAL_CONFIG_LIBS}" ) 90 | # MESSAGE("DBG GDAL_LIB_NAME_WITH_PREFIX=${GDAL_LIB_NAME_WITH_PREFIX}") 91 | 92 | 93 | ## remove prefix -l because we need the pure name 94 | 95 | IF (GDAL_LIB_NAME_WITH_PREFIX) 96 | STRING(REGEX REPLACE "[-][l]" "" GDAL_LIB_NAME ${GDAL_LIB_NAME_WITH_PREFIX} ) 97 | ENDIF (GDAL_LIB_NAME_WITH_PREFIX) 98 | 99 | IF (APPLE) 100 | SET(GDAL_LIBRARY ${GDAL_LINK_DIRECTORIES}/lib${GDAL_LIB_NAME}.dylib CACHE STRING INTERNAL) 101 | ELSE (APPLE) 102 | SET(GDAL_LIBRARY ${GDAL_LINK_DIRECTORIES}/lib${GDAL_LIB_NAME}.so CACHE STRING INTERNAL) 103 | ENDIF (APPLE) 104 | 105 | ELSE(GDAL_CONFIG) 106 | MESSAGE("FindGDAL.cmake: gdal-config not found. Please set it manually. GDAL_CONFIG=${GDAL_CONFIG}") 107 | ENDIF(GDAL_CONFIG) 108 | 109 | ENDIF(UNIX) 110 | ENDIF(WIN32) 111 | 112 | 113 | IF (GDAL_INCLUDE_DIR AND GDAL_LIBRARY) 114 | SET(GDAL_FOUND TRUE) 115 | ENDIF (GDAL_INCLUDE_DIR AND GDAL_LIBRARY) 116 | 117 | IF (GDAL_FOUND) 118 | 119 | IF (NOT GDAL_FIND_QUIETLY) 120 | MESSAGE(STATUS "Found GDAL: ${GDAL_LIBRARY}") 121 | ENDIF (NOT GDAL_FIND_QUIETLY) 122 | 123 | ELSE (GDAL_FOUND) 124 | 125 | MESSAGE(GDAL_INCLUDE_DIR=${GDAL_INCLUDE_DIR}) 126 | MESSAGE(GDAL_LIBRARY=${GDAL_LIBRARY}) 127 | MESSAGE(FATAL_ERROR "Could not find GDAL") 128 | 129 | ENDIF (GDAL_FOUND) 130 | -------------------------------------------------------------------------------- /3_basic_labelling/cmake_find_rules/FindGEOS.cmake: -------------------------------------------------------------------------------- 1 | 2 | # CMake module to search for GEOS library 3 | # 4 | # If it's found it sets GEOS_FOUND to TRUE 5 | # and following variables are set: 6 | # GEOS_INCLUDE_DIR 7 | # GEOS_LIBRARY 8 | 9 | 10 | FIND_PATH(GEOS_INCLUDE_DIR geos.h 11 | /usr/local/include 12 | /usr/include 13 | #MSVC 14 | "$ENV{LIB_DIR}/include" 15 | #mingw 16 | c:/msys/local/include 17 | ) 18 | 19 | FIND_LIBRARY(GEOS_LIBRARY NAMES geos PATHS 20 | /usr/local/lib 21 | /usr/lib 22 | #MSVC 23 | "$ENV{LIB_DIR}/lib" 24 | #mingw 25 | c:/msys/local/lib 26 | ) 27 | 28 | IF (GEOS_INCLUDE_DIR AND GEOS_LIBRARY) 29 | SET(GEOS_FOUND TRUE) 30 | ENDIF (GEOS_INCLUDE_DIR AND GEOS_LIBRARY) 31 | 32 | 33 | IF (GEOS_FOUND) 34 | 35 | IF (NOT GEOS_FIND_QUIETLY) 36 | MESSAGE(STATUS "Found GEOS: ${GEOS_LIBRARY}") 37 | ENDIF (NOT GEOS_FIND_QUIETLY) 38 | 39 | ELSE (GEOS_FOUND) 40 | 41 | IF (GEOS_FIND_REQUIRED) 42 | MESSAGE(FATAL_ERROR "Could not find GEOS") 43 | ENDIF (GEOS_FIND_REQUIRED) 44 | 45 | ENDIF (GEOS_FOUND) 46 | -------------------------------------------------------------------------------- /3_basic_labelling/cmake_find_rules/FindQGIS.cmake: -------------------------------------------------------------------------------- 1 | ## Once run this will define: 2 | ## 3 | ## QGIS_FOUND = system has QGIS lib 4 | ## 5 | ## QGIS_CORE_LIBRARY = full path to the CORE library 6 | ## QGIS_GUI_LIBRARY = full path to the GUI library 7 | ## QGIS_PLUGIN_DIR = full path to where QGIS plugins are installed 8 | ## QGIS_INCLUDE_DIR = where to find headers 9 | ## 10 | ## Tim Sutton 11 | 12 | #MESSAGE("Searching for QGIS") 13 | IF(WIN32) 14 | #MESSAGE("Searching for QGIS in C:/program files/Quantum GIS") 15 | IF (MINGW) 16 | FIND_PATH(QGIS_PLUGIN_DIR libnortharrowplugin.dll 17 | "C:/Program Files/Quantum GIS/plugins" 18 | ) 19 | FIND_PATH(QGIS_INCLUDE_DIR qgsapplication.h 20 | "C:/Program Files/Quantum GIS/include" 21 | ) 22 | FIND_LIBRARY(QGIS_CORE_LIBRARY NAMES qgis_core PATHS 23 | "C:/Program Files/Quantum GIS/" 24 | ) 25 | FIND_LIBRARY(QGIS_GUI_LIBRARY NAMES qgis_gui PATHS 26 | "C:/Program Files/Quantum GIS/" 27 | ) 28 | ENDIF (MINGW) 29 | 30 | IF (MSVC) 31 | FIND_PATH(QGIS_PLUGIN_DIR libnortharrowplugin.dll 32 | "C:/Program Files/Quantum GIS/lib/qgis" 33 | ) 34 | FIND_PATH(QGIS_INCLUDE_DIR qgsapplication.h 35 | "$ENV{LIB_DIR}/include/qgis" 36 | ) 37 | FIND_LIBRARY(QGIS_CORE_LIBRARY NAMES qgis_core PATHS 38 | "$ENV{LIB_DIR}/lib/" 39 | ) 40 | FIND_LIBRARY(QGIS_GUI_LIBRARY NAMES qgis_gui PATHS 41 | "$ENV{LIB_DIR}/lib/" 42 | ) 43 | ENDIF (MSVC) 44 | 45 | ELSE(WIN32) 46 | IF(UNIX) 47 | 48 | # try to use bundle on mac 49 | IF (APPLE) 50 | #MESSAGE("Searching for QGIS in /Applications/QGIS.app/Contents/MacOS") 51 | SET (QGIS_MAC_PATH /Applications/QGIS1.0.0.app/Contents/MacOS) 52 | SET (QGIS_LIB_DIR ${QGIS_MAC_PATH}/lib) 53 | SET (QGIS_PLUGIN_DIR ${QGIS_MAC_PATH}/lib/qgis CACHE STRING INTERNAL) 54 | # set INCLUDE_DIR to prefix+include 55 | SET(QGIS_INCLUDE_DIR ${QGIS_MAC_PATH}/include/qgis CACHE STRING INTERNAL) 56 | ## extract link dirs 57 | SET(QGIS_CORE_LIBRARY ${QGIS_LIB_DIR}/libqgis_core.dylib CACHE STRING INTERNAL) 58 | SET(QGIS_GUI_LIBRARY ${QGIS_LIB_DIR}/libqgis_gui.dylib CACHE STRING INTERNAL) 59 | ELSE (APPLE) # Unix / Linux 60 | #MESSAGE("Searching for QGIS in /usr/bin; /usr/local/bin") 61 | FIND_PATH(QGIS_PLUGIN_DIR libnortharrowplugin.so 62 | /usr/lib/qgis 63 | /usr/local/lib/qgis 64 | "$ENV{LIB_DIR}/lib/qgis" 65 | ) 66 | FIND_PATH(QGIS_INCLUDE_DIR qgis.h 67 | /usr/include/qgis 68 | /usr/local/include/qgis 69 | "$ENV{LIB_DIR}/include/qgis" 70 | ) 71 | FIND_LIBRARY(QGIS_CORE_LIBRARY NAMES qgis_core PATHS 72 | /usr/lib 73 | /usr/local/lib 74 | "$ENV{LIB_DIR}/lib" 75 | ) 76 | FIND_LIBRARY(QGIS_GUI_LIBRARY NAMES qgis_gui PATHS 77 | /usr/lib 78 | /usr/local/lib 79 | "$ENV{LIB_DIR}/lib" 80 | ) 81 | ENDIF (APPLE) 82 | ENDIF(UNIX) 83 | ENDIF(WIN32) 84 | 85 | 86 | IF (QGIS_INCLUDE_DIR AND QGIS_CORE_LIBRARY AND QGIS_GUI_LIBRARY) 87 | SET(QGIS_FOUND TRUE) 88 | ENDIF (QGIS_INCLUDE_DIR AND QGIS_CORE_LIBRARY AND QGIS_GUI_LIBRARY) 89 | 90 | IF (QGIS_FOUND) 91 | IF (NOT QGIS_FIND_QUIETLY) 92 | MESSAGE(STATUS "Found QGIS Core: ${QGIS_CORE_LIBRARY}") 93 | MESSAGE(STATUS "Found QGIS Gui: ${QGIS_GUI_LIBRARY}") 94 | MESSAGE(STATUS "Found QGIS Plugins Dir: ${QGIS_PLUGIN_DIR}") 95 | ENDIF (NOT QGIS_FIND_QUIETLY) 96 | ELSE (QGIS_FOUND) 97 | IF (QGIS_FIND_REQUIRED) 98 | MESSAGE(FATAL_ERROR "Could not find QGIS") 99 | ENDIF (QGIS_FIND_REQUIRED) 100 | ENDIF (QGIS_FOUND) 101 | -------------------------------------------------------------------------------- /3_basic_labelling/data/test.dbf: -------------------------------------------------------------------------------- 1 | _AQNameCP Bob QGIS Rocks Tim Pumpkin Aru Savledore Happy Happy -------------------------------------------------------------------------------- /3_basic_labelling/data/test.prj: -------------------------------------------------------------------------------- 1 | GEOGCS["GCS_WGS_1984",DATUM["D_unknown",SPHEROID["WGS84",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]] -------------------------------------------------------------------------------- /3_basic_labelling/data/test.shp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/3_basic_labelling/data/test.shp -------------------------------------------------------------------------------- /3_basic_labelling/data/test.shx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/3_basic_labelling/data/test.shx -------------------------------------------------------------------------------- /3_basic_labelling/mActionAddLayer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/3_basic_labelling/mActionAddLayer.png -------------------------------------------------------------------------------- /3_basic_labelling/mActionPan.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/3_basic_labelling/mActionPan.png -------------------------------------------------------------------------------- /3_basic_labelling/mActionZoomIn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/3_basic_labelling/mActionZoomIn.png -------------------------------------------------------------------------------- /3_basic_labelling/mActionZoomOut.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/3_basic_labelling/mActionZoomOut.png -------------------------------------------------------------------------------- /3_basic_labelling/main.cpp: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * Copyright (C) 2006 by Tim Sutton * 3 | * tim@linfiniti.com * 4 | * * 5 | * This program is free software; you can redistribute it and/or modify * 6 | * it under the terms of the GNU General Public License as published by * 7 | * the Free Software Foundation; either version 2 of the License, or * 8 | * (at your option) any later version. * 9 | * * 10 | * This program is distributed in the hope that it will be useful, * 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 13 | * GNU General Public License for more details. * 14 | * * 15 | * You should have received a copy of the GNU General Public License * 16 | * along with this program; if not, write to the * 17 | * Free Software Foundation, Inc., * 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 19 | ***************************************************************************/ 20 | // 21 | // Qt Includes 22 | // 23 | #include 24 | #include 25 | // 26 | // QGIS Includes 27 | // 28 | #include 29 | 30 | int main(int argc, char ** argv) 31 | { 32 | // Start the Application 33 | QgsApplication app(argc, argv, TRUE); 34 | MainWindow * mypMainWindow = new MainWindow(); 35 | mypMainWindow->show(); 36 | // Start the Application Event Loop 37 | return app.exec(); 38 | } 39 | -------------------------------------------------------------------------------- /3_basic_labelling/mainwindow.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * Copyright (C) 2006 by Tim Sutton * 3 | * tim@linfiniti.com * 4 | * * 5 | * This program is free software; you can redistribute it and/or modify * 6 | * it under the terms of the GNU General Public License as published by * 7 | * the Free Software Foundation; either version 2 of the License, or * 8 | * (at your option) any later version. * 9 | * * 10 | * This program is distributed in the hope that it will be useful, * 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 13 | * GNU General Public License for more details. * 14 | * * 15 | * You should have received a copy of the GNU General Public License * 16 | * along with this program; if not, write to the * 17 | * Free Software Foundation, Inc., * 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 19 | ***************************************************************************/ 20 | #ifndef OMGMAINWINDOW_H 21 | #define OMGMAINWINDOW_H 22 | 23 | //QGis Includes 24 | #include 25 | #include 26 | 27 | //QT Includes 28 | #include 29 | 30 | //Local Includes 31 | #include 32 | 33 | /** 34 | @author Tim Sutton 35 | */ 36 | class MainWindow : public QMainWindow, private Ui::MainWindowBase 37 | { 38 | Q_OBJECT; 39 | public: 40 | MainWindow(QWidget* parent = 0, Qt::WFlags fl = 0 ); 41 | ~MainWindow(); 42 | public slots: 43 | void zoomInMode(); 44 | void zoomOutMode(); 45 | void panMode(); 46 | void addLayer(); 47 | 48 | private: 49 | QgsMapCanvas * mpMapCanvas; 50 | QVBoxLayout * mpLayout; 51 | QToolBar * mpMapToolBar; 52 | QgsMapTool * mpPanTool; 53 | QgsMapTool * mpZoomInTool; 54 | QgsMapTool * mpZoomOutTool; 55 | }; 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /3_basic_labelling/mainwindowbase.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | MainWindowBase 6 | 7 | 8 | 9 | 0 10 | 0 11 | 579 12 | 330 13 | 14 | 15 | 16 | MainWindow 17 | 18 | 19 | 20 | 21 | 9 22 | 23 | 24 | 6 25 | 26 | 27 | 28 | 29 | QFrame::StyledPanel 30 | 31 | 32 | QFrame::Raised 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 0 42 | 0 43 | 579 44 | 22 45 | 46 | 47 | 48 | 49 | Map 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | :/mActionZoomIn.png 62 | 63 | 64 | Zoom In 65 | 66 | 67 | 68 | 69 | :/mActionZoomOut.png 70 | 71 | 72 | Zoom Out 73 | 74 | 75 | 76 | 77 | :/mActionPan.png 78 | 79 | 80 | Pan 81 | 82 | 83 | 84 | 85 | :/mActionAddLayer.png 86 | 87 | 88 | Add Layer 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | -------------------------------------------------------------------------------- /3_basic_labelling/resources.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | mActionAddLayer.png 4 | mActionPan.png 5 | mActionZoomIn.png 6 | mActionZoomOut.png 7 | 8 | 9 | -------------------------------------------------------------------------------- /4_adding_rasters_to_canvas/4_adding_rasters_to_canvas.pro: -------------------------------------------------------------------------------- 1 | TEMPLATE = app 2 | TARGET = qgis_example4 3 | QT = qt3support sql network svg gui core xml 4 | LANGUAGE= C++ 5 | linux-g++{ 6 | QGISDIR=[path to installed qgis] 7 | QGISLIBDIR=$${QGISDIR}/lib 8 | QGISSRCDIR=[path to qgis src directory] 9 | QGISPLUGINDIR=$${QGISLIBDIR}/qgis 10 | DEFINES += QGISPLUGINDIR=$${QGISPLUGINDIR} 11 | LIBS = -L$${QGISLIBDIR} -lqgis_core -lqgis_gui -lproj 12 | } 13 | macx{ 14 | QGISDIR=/Applications/qgis.app/Contents/MacOS/ 15 | QGISLIBDIR=$${QGISDIR}/lib 16 | QGISSRCDIR=/Users/timsutton/dev/cpp/qgis/src/ 17 | QGISPLUGINDIR=$${QGISLIBDIR}/qgis 18 | DEFINES += QGISPLUGINDIR=$${QGISPLUGINDIR} 19 | LIBS = -L$${QGISLIBDIR} -lqgis_core -lqgis_gui \ 20 | -L/Library/Frameworks/GDAL.framework/unix/lib/ 21 | INCLUDEPATH += /Library/Frameworks/GDAL.framework/Headers 22 | } 23 | 24 | INCLUDEPATH += $${QGISDIR}/include/qgis #\ 25 | #$${QGISSRCDIR}/core \ 26 | #$${QGISSRCDIR}/gui \ 27 | #$${QGISSRCDIR}/plugins \ 28 | #$${QGISSRCDIR}/providers \ 29 | #$${QGISSRCDIR}/raster \ 30 | #$${QGISSRCDIR}/ui 31 | 32 | DEFINES += CORE_EXPORT="" 33 | DEFINES += GUI_EXPORT="" 34 | 35 | CONFIG += qt gui exceptions stl warn_on debug thread 36 | 37 | RESOURCES += resources.qrc 38 | 39 | FORMS += mainwindowbase.ui 40 | 41 | HEADERS = mainwindow.h 42 | 43 | SOURCES = main.cpp \ 44 | mainwindow.cpp 45 | 46 | -------------------------------------------------------------------------------- /4_adding_rasters_to_canvas/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.6) 2 | PROJECT(timtut4) 3 | SET(CMAKE_COLOR_MAKEFILE ON) 4 | # set path to additional CMake modules 5 | SET(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake_find_rules ${CMAKE_MODULE_PATH}) 6 | FIND_PACKAGE(QGIS REQUIRED) 7 | IF (NOT QGIS_FOUND) 8 | MESSAGE (SEND_ERROR "QGIS dependency was not found!") 9 | ENDIF (NOT QGIS_FOUND) 10 | IF (WIN32) 11 | # expect that classes are being imported by default 12 | # Note: MSVC doesn't like when the macros are quotes 13 | # and MSYS doesn't like them unqouted (bacause of braces) 14 | # import qgis classes 15 | IF (MSVC) 16 | ADD_DEFINITIONS("-DGUI_EXPORT=__declspec(dllimport)") 17 | ADD_DEFINITIONS("-DCORE_EXPORT=__declspec(dllimport)") 18 | ELSE (MSVC) 19 | ADD_DEFINITIONS("\"-DGUI_EXPORT=__declspec(dllimport)\"") 20 | ADD_DEFINITIONS("\"-DCORE_EXPORT=__declspec(dllimport)\"") 21 | ENDIF (MSVC) 22 | ELSE (WIN32) 23 | ADD_DEFINITIONS(-DGUI_EXPORT=) 24 | ADD_DEFINITIONS(-DCORE_EXPORT=) 25 | ENDIF (WIN32) 26 | 27 | FIND_PACKAGE(GDAL REQUIRED) 28 | IF (NOT GDAL_FOUND) 29 | MESSAGE (SEND_ERROR "GDAL dependency was not found!") 30 | ENDIF (NOT GDAL_FOUND) 31 | 32 | FIND_PACKAGE(GEOS REQUIRED) 33 | IF (NOT GEOS_FOUND) 34 | MESSAGE (SEND_ERROR "GEOS dependency was not found!") 35 | ENDIF (NOT GEOS_FOUND) 36 | 37 | IF (CMAKE_BUILD_TYPE MATCHES Debug) 38 | ADD_DEFINITIONS(-Dtimtut4DEBUG=1) 39 | ENDIF (CMAKE_BUILD_TYPE MATCHES Debug) 40 | ######################################################## 41 | # Files 42 | 43 | SET (timtut4_SRCS 44 | main.cpp 45 | mainwindow.cpp 46 | ) 47 | 48 | # This tut uses no UIs 49 | SET (timtut4_UIS 50 | mainwindowbase.ui 51 | ) 52 | 53 | # This tut needs no MOC 54 | SET (timtut4_MOC_HDRS 55 | mainwindow.h 56 | ) 57 | 58 | 59 | SET (timtut4_RCCS 60 | resources.qrc 61 | ) 62 | 63 | SET (QT_USE_QT3SUPPORT FALSE) 64 | SET (QT_USE_QTGUI TRUE) 65 | SET (QT_USE_QTSQL TRUE) 66 | SET (QT_USE_QTSVG TRUE) 67 | SET (QT_USE_QTXML TRUE) 68 | SET (QT_USE_QTNETWORK TRUE) 69 | FIND_PACKAGE(Qt4 REQUIRED) 70 | INCLUDE( ${QT_USE_FILE} ) 71 | ######################################################## 72 | # Build 73 | 74 | QT4_WRAP_UI (timtut4_UIS_H ${timtut4_UIS}) 75 | 76 | QT4_WRAP_CPP (timtut4_MOC_SRCS ${timtut4_MOC_HDRS}) 77 | 78 | QT4_ADD_RESOURCES(timtut4_RCC_SRCS ${timtut4_RCCS}) 79 | 80 | ADD_EXECUTABLE (timtut4 ${timtut4_SRCS} ${timtut4_MOC_SRCS} ${timtut4_RCC_SRCS} ${timtut4_UIS_H}) 81 | 82 | INCLUDE_DIRECTORIES( 83 | ${GDAL_INCLUDE_DIR} 84 | ${GEOS_INCLUDE_DIR} 85 | ${CMAKE_CURRENT_BINARY_DIR} 86 | ${QT_INCLUDE_DIR} 87 | ${QGIS_INCLUDE_DIR} 88 | . 89 | ) 90 | 91 | #This is probably no longer needed, but I will leave it in for Win machines for the moment 92 | IF(QT_QTSQL_FOUND) 93 | FIND_LIBRARY(QT_QTSQL_LIBRARY NAMES QtSql QtSql4 PATHS ${QT_LIBRARY_DIR} NO_DEFAULT_PATH) 94 | SET(QT_LIBRARIES ${QT_LIBRARIES} ${QT_QTSQL_LIBRARY}) 95 | ENDIF(QT_QTSQL_FOUND) 96 | 97 | TARGET_LINK_LIBRARIES(timtut4 98 | ${QT_LIBRARIES} 99 | ${QGIS_CORE_LIBRARY} 100 | ${QGIS_GUI_LIBRARY} 101 | ${GEOS_LIBRARY} 102 | ${GDAL_LIBRARY} 103 | ) 104 | 105 | IF (MSVC) 106 | # Very important or you get all kinds of odd side 107 | #effects like app crash on start up saying qtgui.dll 108 | TARGET_LINK_LIBRARIES( timtut4 109 | qtmain 110 | ) 111 | ENDIF (MSVC) 112 | 113 | ######################################################## 114 | # Install 115 | 116 | 117 | -------------------------------------------------------------------------------- /4_adding_rasters_to_canvas/build.mac.sh: -------------------------------------------------------------------------------- 1 | #/bin/bash 2 | qmake 3 | make 4 | mkdir qgis_example4.app/Contents/MacOS/lib/ 5 | cp /Applications/qgis.app/Contents/MacOS/lib/libqgis_core.dylib qgis_example4.app/Contents/MacOS/lib/ 6 | cp /Applications/qgis.app/Contents/MacOS/lib/libqgis_gui.dylib qgis_example4.app/Contents/MacOS/lib/ 7 | cp -r data qgis_example4.app/Contents/MacOS/ 8 | mkdir -p qgis_example4.app/Contents/MacOS/share/qgis/resources/ 9 | cp /Applications/qgis.app/Contents/MacOS/share/qgis/resources/* qgis_example4.app/Contents/MacOS/share/qgis/resources/ 10 | -------------------------------------------------------------------------------- /4_adding_rasters_to_canvas/cmake_find_rules/FindGDAL.cmake: -------------------------------------------------------------------------------- 1 | ## Once run this will define: 2 | ## 3 | ## GDAL_FOUND = system has GDAL lib 4 | ## 5 | ## GDAL_LIBRARY = full path to the library 6 | ## 7 | ## GDAL_INCLUDE_DIR = where to find headers 8 | ## 9 | ## Magnus Homann 10 | 11 | 12 | IF(WIN32) 13 | 14 | IF (MINGW) 15 | FIND_PATH(GDAL_INCLUDE_DIR gdal.h /usr/local/include /usr/include c:/msys/local/include) 16 | FIND_LIBRARY(GDAL_LIBRARY NAMES gdal PATHS /usr/local/lib /usr/lib c:/msys/local/lib) 17 | ENDIF (MINGW) 18 | 19 | IF (MSVC) 20 | FIND_PATH(GDAL_INCLUDE_DIR gdal.h 21 | "$ENV{LIB_DIR}/include/gdal" 22 | ) 23 | FIND_LIBRARY(GDAL_LIBRARY NAMES gdal PATHS 24 | "$ENV{LIB_DIR}/lib/" 25 | ) 26 | 27 | # NOTE: under msvc you should add the following to your target link libraries 28 | # list as they are required by gdal 29 | #odbc32 odbccp32 30 | 31 | ENDIF (MSVC) 32 | 33 | 34 | ELSE(WIN32) 35 | IF(UNIX) 36 | 37 | # try to use framework on mac 38 | IF (APPLE) 39 | SET (GDAL_MAC_PATH /Library/Frameworks/GDAL.framework/unix/bin) 40 | ENDIF (APPLE) 41 | 42 | SET(GDAL_CONFIG_PREFER_PATH "$ENV{GDAL_HOME}/bin" CACHE STRING "preferred path to GDAL (gdal-config)") 43 | FIND_PROGRAM(GDAL_CONFIG gdal-config 44 | ${GDAL_CONFIG_PREFER_PATH} 45 | ${GDAL_MAC_PATH} 46 | /usr/local/bin/ 47 | /usr/bin/ 48 | ) 49 | # MESSAGE("DBG GDAL_CONFIG ${GDAL_CONFIG}") 50 | 51 | IF (GDAL_CONFIG) 52 | # set INCLUDE_DIR to prefix+include 53 | EXEC_PROGRAM(${GDAL_CONFIG} 54 | ARGS --prefix 55 | OUTPUT_VARIABLE GDAL_PREFIX) 56 | #SET(GDAL_INCLUDE_DIR ${GDAL_PREFIX}/include CACHE STRING INTERNAL) 57 | FIND_PATH(GDAL_INCLUDE_DIR 58 | gdal.h 59 | ${GDAL_PREFIX}/include/gdal 60 | ${GDAL_PREFIX}/include 61 | /usr/local/include 62 | /usr/include 63 | ) 64 | 65 | ## extract link dirs for rpath 66 | EXEC_PROGRAM(${GDAL_CONFIG} 67 | ARGS --libs 68 | OUTPUT_VARIABLE GDAL_CONFIG_LIBS ) 69 | 70 | ## split off the link dirs (for rpath) 71 | ## use regular expression to match wildcard equivalent "-L*" 72 | ## with is a space or a semicolon 73 | STRING(REGEX MATCHALL "[-][L]([^ ;])+" 74 | GDAL_LINK_DIRECTORIES_WITH_PREFIX 75 | "${GDAL_CONFIG_LIBS}" ) 76 | # MESSAGE("DBG GDAL_LINK_DIRECTORIES_WITH_PREFIX=${GDAL_LINK_DIRECTORIES_WITH_PREFIX}") 77 | 78 | ## remove prefix -L because we need the pure directory for LINK_DIRECTORIES 79 | 80 | IF (GDAL_LINK_DIRECTORIES_WITH_PREFIX) 81 | STRING(REGEX REPLACE "[-][L]" "" GDAL_LINK_DIRECTORIES ${GDAL_LINK_DIRECTORIES_WITH_PREFIX} ) 82 | ENDIF (GDAL_LINK_DIRECTORIES_WITH_PREFIX) 83 | 84 | ## split off the name 85 | ## use regular expression to match wildcard equivalent "-l*" 86 | ## with is a space or a semicolon 87 | STRING(REGEX MATCHALL "[-][l]([^ ;])+" 88 | GDAL_LIB_NAME_WITH_PREFIX 89 | "${GDAL_CONFIG_LIBS}" ) 90 | # MESSAGE("DBG GDAL_LIB_NAME_WITH_PREFIX=${GDAL_LIB_NAME_WITH_PREFIX}") 91 | 92 | 93 | ## remove prefix -l because we need the pure name 94 | 95 | IF (GDAL_LIB_NAME_WITH_PREFIX) 96 | STRING(REGEX REPLACE "[-][l]" "" GDAL_LIB_NAME ${GDAL_LIB_NAME_WITH_PREFIX} ) 97 | ENDIF (GDAL_LIB_NAME_WITH_PREFIX) 98 | 99 | IF (APPLE) 100 | SET(GDAL_LIBRARY ${GDAL_LINK_DIRECTORIES}/lib${GDAL_LIB_NAME}.dylib CACHE STRING INTERNAL) 101 | ELSE (APPLE) 102 | SET(GDAL_LIBRARY ${GDAL_LINK_DIRECTORIES}/lib${GDAL_LIB_NAME}.so CACHE STRING INTERNAL) 103 | ENDIF (APPLE) 104 | 105 | ELSE(GDAL_CONFIG) 106 | MESSAGE("FindGDAL.cmake: gdal-config not found. Please set it manually. GDAL_CONFIG=${GDAL_CONFIG}") 107 | ENDIF(GDAL_CONFIG) 108 | 109 | ENDIF(UNIX) 110 | ENDIF(WIN32) 111 | 112 | 113 | IF (GDAL_INCLUDE_DIR AND GDAL_LIBRARY) 114 | SET(GDAL_FOUND TRUE) 115 | ENDIF (GDAL_INCLUDE_DIR AND GDAL_LIBRARY) 116 | 117 | IF (GDAL_FOUND) 118 | 119 | IF (NOT GDAL_FIND_QUIETLY) 120 | MESSAGE(STATUS "Found GDAL: ${GDAL_LIBRARY}") 121 | ENDIF (NOT GDAL_FIND_QUIETLY) 122 | 123 | ELSE (GDAL_FOUND) 124 | 125 | MESSAGE(GDAL_INCLUDE_DIR=${GDAL_INCLUDE_DIR}) 126 | MESSAGE(GDAL_LIBRARY=${GDAL_LIBRARY}) 127 | MESSAGE(FATAL_ERROR "Could not find GDAL") 128 | 129 | ENDIF (GDAL_FOUND) 130 | -------------------------------------------------------------------------------- /4_adding_rasters_to_canvas/cmake_find_rules/FindGEOS.cmake: -------------------------------------------------------------------------------- 1 | 2 | # CMake module to search for GEOS library 3 | # 4 | # If it's found it sets GEOS_FOUND to TRUE 5 | # and following variables are set: 6 | # GEOS_INCLUDE_DIR 7 | # GEOS_LIBRARY 8 | 9 | 10 | FIND_PATH(GEOS_INCLUDE_DIR geos.h 11 | /usr/local/include 12 | /usr/include 13 | #MSVC 14 | "$ENV{LIB_DIR}/include" 15 | #mingw 16 | c:/msys/local/include 17 | ) 18 | 19 | FIND_LIBRARY(GEOS_LIBRARY NAMES geos PATHS 20 | /usr/local/lib 21 | /usr/lib 22 | #MSVC 23 | "$ENV{LIB_DIR}/lib" 24 | #mingw 25 | c:/msys/local/lib 26 | ) 27 | 28 | IF (GEOS_INCLUDE_DIR AND GEOS_LIBRARY) 29 | SET(GEOS_FOUND TRUE) 30 | ENDIF (GEOS_INCLUDE_DIR AND GEOS_LIBRARY) 31 | 32 | 33 | IF (GEOS_FOUND) 34 | 35 | IF (NOT GEOS_FIND_QUIETLY) 36 | MESSAGE(STATUS "Found GEOS: ${GEOS_LIBRARY}") 37 | ENDIF (NOT GEOS_FIND_QUIETLY) 38 | 39 | ELSE (GEOS_FOUND) 40 | 41 | IF (GEOS_FIND_REQUIRED) 42 | MESSAGE(FATAL_ERROR "Could not find GEOS") 43 | ENDIF (GEOS_FIND_REQUIRED) 44 | 45 | ENDIF (GEOS_FOUND) 46 | -------------------------------------------------------------------------------- /4_adding_rasters_to_canvas/cmake_find_rules/FindQGIS.cmake: -------------------------------------------------------------------------------- 1 | ## Once run this will define: 2 | ## 3 | ## QGIS_FOUND = system has QGIS lib 4 | ## 5 | ## QGIS_CORE_LIBRARY = full path to the CORE library 6 | ## QGIS_GUI_LIBRARY = full path to the GUI library 7 | ## QGIS_PLUGIN_DIR = full path to where QGIS plugins are installed 8 | ## QGIS_INCLUDE_DIR = where to find headers 9 | ## 10 | ## Tim Sutton 11 | 12 | #MESSAGE("Searching for QGIS") 13 | IF(WIN32) 14 | #MESSAGE("Searching for QGIS in C:/program files/Quantum GIS") 15 | IF (MINGW) 16 | FIND_PATH(QGIS_PLUGIN_DIR libnortharrowplugin.dll 17 | "C:/Program Files/Quantum GIS/plugins" 18 | ) 19 | FIND_PATH(QGIS_INCLUDE_DIR qgsapplication.h 20 | "C:/Program Files/Quantum GIS/include" 21 | ) 22 | FIND_LIBRARY(QGIS_CORE_LIBRARY NAMES qgis_core PATHS 23 | "C:/Program Files/Quantum GIS/" 24 | ) 25 | FIND_LIBRARY(QGIS_GUI_LIBRARY NAMES qgis_gui PATHS 26 | "C:/Program Files/Quantum GIS/" 27 | ) 28 | ENDIF (MINGW) 29 | 30 | IF (MSVC) 31 | FIND_PATH(QGIS_PLUGIN_DIR libnortharrowplugin.dll 32 | "C:/Program Files/Quantum GIS/lib/qgis" 33 | ) 34 | FIND_PATH(QGIS_INCLUDE_DIR qgsapplication.h 35 | "$ENV{LIB_DIR}/include/qgis" 36 | ) 37 | FIND_LIBRARY(QGIS_CORE_LIBRARY NAMES qgis_core PATHS 38 | "$ENV{LIB_DIR}/lib/" 39 | ) 40 | FIND_LIBRARY(QGIS_GUI_LIBRARY NAMES qgis_gui PATHS 41 | "$ENV{LIB_DIR}/lib/" 42 | ) 43 | ENDIF (MSVC) 44 | 45 | ELSE(WIN32) 46 | IF(UNIX) 47 | 48 | # try to use bundle on mac 49 | IF (APPLE) 50 | #MESSAGE("Searching for QGIS in /Applications/QGIS.app/Contents/MacOS") 51 | SET (QGIS_MAC_PATH /Applications/QGIS1.0.0.app/Contents/MacOS) 52 | SET (QGIS_LIB_DIR ${QGIS_MAC_PATH}/lib) 53 | SET (QGIS_PLUGIN_DIR ${QGIS_MAC_PATH}/lib/qgis CACHE STRING INTERNAL) 54 | # set INCLUDE_DIR to prefix+include 55 | SET(QGIS_INCLUDE_DIR ${QGIS_MAC_PATH}/include/qgis CACHE STRING INTERNAL) 56 | ## extract link dirs 57 | SET(QGIS_CORE_LIBRARY ${QGIS_LIB_DIR}/libqgis_core.dylib CACHE STRING INTERNAL) 58 | SET(QGIS_GUI_LIBRARY ${QGIS_LIB_DIR}/libqgis_gui.dylib CACHE STRING INTERNAL) 59 | ELSE (APPLE) # Unix / Linux 60 | #MESSAGE("Searching for QGIS in /usr/bin; /usr/local/bin") 61 | FIND_PATH(QGIS_PLUGIN_DIR libnortharrowplugin.so 62 | /usr/lib/qgis 63 | /usr/local/lib/qgis 64 | "$ENV{LIB_DIR}/lib/qgis" 65 | ) 66 | FIND_PATH(QGIS_INCLUDE_DIR qgis.h 67 | /usr/include/qgis 68 | /usr/local/include/qgis 69 | "$ENV{LIB_DIR}/include/qgis" 70 | ) 71 | FIND_LIBRARY(QGIS_CORE_LIBRARY NAMES qgis_core PATHS 72 | /usr/lib 73 | /usr/local/lib 74 | "$ENV{LIB_DIR}/lib" 75 | ) 76 | FIND_LIBRARY(QGIS_GUI_LIBRARY NAMES qgis_gui PATHS 77 | /usr/lib 78 | /usr/local/lib 79 | "$ENV{LIB_DIR}/lib" 80 | ) 81 | ENDIF (APPLE) 82 | ENDIF(UNIX) 83 | ENDIF(WIN32) 84 | 85 | 86 | IF (QGIS_INCLUDE_DIR AND QGIS_CORE_LIBRARY AND QGIS_GUI_LIBRARY) 87 | SET(QGIS_FOUND TRUE) 88 | ENDIF (QGIS_INCLUDE_DIR AND QGIS_CORE_LIBRARY AND QGIS_GUI_LIBRARY) 89 | 90 | IF (QGIS_FOUND) 91 | IF (NOT QGIS_FIND_QUIETLY) 92 | MESSAGE(STATUS "Found QGIS Core: ${QGIS_CORE_LIBRARY}") 93 | MESSAGE(STATUS "Found QGIS Gui: ${QGIS_GUI_LIBRARY}") 94 | MESSAGE(STATUS "Found QGIS Plugins Dir: ${QGIS_PLUGIN_DIR}") 95 | ENDIF (NOT QGIS_FIND_QUIETLY) 96 | ELSE (QGIS_FOUND) 97 | IF (QGIS_FIND_REQUIRED) 98 | MESSAGE(FATAL_ERROR "Could not find QGIS") 99 | ENDIF (QGIS_FIND_REQUIRED) 100 | ENDIF (QGIS_FOUND) 101 | -------------------------------------------------------------------------------- /4_adding_rasters_to_canvas/data/Abarema_jupunba_projection.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/4_adding_rasters_to_canvas/data/Abarema_jupunba_projection.tif -------------------------------------------------------------------------------- /4_adding_rasters_to_canvas/mActionAddLayer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/4_adding_rasters_to_canvas/mActionAddLayer.png -------------------------------------------------------------------------------- /4_adding_rasters_to_canvas/mActionPan.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/4_adding_rasters_to_canvas/mActionPan.png -------------------------------------------------------------------------------- /4_adding_rasters_to_canvas/mActionZoomIn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/4_adding_rasters_to_canvas/mActionZoomIn.png -------------------------------------------------------------------------------- /4_adding_rasters_to_canvas/mActionZoomOut.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/4_adding_rasters_to_canvas/mActionZoomOut.png -------------------------------------------------------------------------------- /4_adding_rasters_to_canvas/main.cpp: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * Copyright (C) 2006 by Tim Sutton * 3 | * tim@linfiniti.com * 4 | * * 5 | * This program is free software; you can redistribute it and/or modify * 6 | * it under the terms of the GNU General Public License as published by * 7 | * the Free Software Foundation; either version 2 of the License, or * 8 | * (at your option) any later version. * 9 | * * 10 | * This program is distributed in the hope that it will be useful, * 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 13 | * GNU General Public License for more details. * 14 | * * 15 | * You should have received a copy of the GNU General Public License * 16 | * along with this program; if not, write to the * 17 | * Free Software Foundation, Inc., * 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 19 | ***************************************************************************/ 20 | // 21 | // Qt Includes 22 | // 23 | #include 24 | #include 25 | // 26 | // QGIS Includes 27 | // 28 | #include 29 | 30 | int main(int argc, char ** argv) 31 | { 32 | // Start the Application 33 | QgsApplication app(argc, argv, TRUE); 34 | MainWindow * mypMainWindow = new MainWindow(); 35 | mypMainWindow->show(); 36 | // Start the Application Event Loop 37 | return app.exec(); 38 | } 39 | -------------------------------------------------------------------------------- /4_adding_rasters_to_canvas/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * Copyright (C) 2006 by Tim Sutton * 3 | * tim@linfiniti.com * 4 | * * 5 | * This program is free software; you can redistribute it and/or modify * 6 | * it under the terms of the GNU General Public License as published by * 7 | * the Free Software Foundation; either version 2 of the License, or * 8 | * (at your option) any later version. * 9 | * * 10 | * This program is distributed in the hope that it will be useful, * 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 13 | * GNU General Public License for more details. * 14 | * * 15 | * You should have received a copy of the GNU General Public License * 16 | * along with this program; if not, write to the * 17 | * Free Software Foundation, Inc., * 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 19 | ***************************************************************************/ 20 | #include "mainwindow.h" 21 | #include 22 | #include 23 | // 24 | // QGIS Includes 25 | // 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | // 33 | // QGIS Map tools 34 | // 35 | #include "qgsmaptoolpan.h" 36 | #include "qgsmaptoolzoom.h" 37 | 38 | MainWindow::MainWindow(QWidget* parent, Qt::WFlags fl) 39 | : QMainWindow(parent,fl) 40 | { 41 | //required by Qt4 to initialise the ui 42 | setupUi(this); 43 | 44 | // Create the Map Canvas 45 | mpMapCanvas= new QgsMapCanvas(0, 0); 46 | mpMapCanvas->enableAntiAliasing(true); 47 | mpMapCanvas->useImageToRender(false); 48 | mpMapCanvas->setCanvasColor(QColor(255, 255, 255)); 49 | mpMapCanvas->freeze(false); 50 | mpMapCanvas->setVisible(true); 51 | mpMapCanvas->refresh(); 52 | mpMapCanvas->show(); 53 | 54 | // Lay our widgets out in the main window 55 | mpLayout = new QVBoxLayout(frameMap); 56 | mpLayout->addWidget(mpMapCanvas); 57 | 58 | //create the action behaviours 59 | connect(mpActionPan, SIGNAL(triggered()), this, SLOT(panMode())); 60 | connect(mpActionZoomIn, SIGNAL(triggered()), this, SLOT(zoomInMode())); 61 | connect(mpActionZoomOut, SIGNAL(triggered()), this, SLOT(zoomOutMode())); 62 | connect(mpActionAddLayer, SIGNAL(triggered()), this, SLOT(addLayer())); 63 | 64 | //create a little toolbar 65 | mpMapToolBar = addToolBar(tr("File")); 66 | mpMapToolBar->addAction(mpActionAddLayer); 67 | mpMapToolBar->addAction(mpActionZoomIn); 68 | mpMapToolBar->addAction(mpActionZoomOut); 69 | mpMapToolBar->addAction(mpActionPan); 70 | 71 | //create the maptools 72 | mpPanTool = new QgsMapToolPan(mpMapCanvas); 73 | mpPanTool->setAction(mpActionPan); 74 | mpZoomInTool = new QgsMapToolZoom(mpMapCanvas, FALSE); // false = in 75 | mpZoomInTool->setAction(mpActionZoomIn); 76 | mpZoomOutTool = new QgsMapToolZoom(mpMapCanvas, TRUE ); //true = out 77 | mpZoomOutTool->setAction(mpActionZoomOut); 78 | } 79 | 80 | MainWindow::~MainWindow() 81 | { 82 | delete mpZoomInTool; 83 | delete mpZoomOutTool; 84 | delete mpPanTool; 85 | delete mpMapToolBar; 86 | delete mpMapCanvas; 87 | delete mpLayout; 88 | } 89 | 90 | void MainWindow::panMode() 91 | { 92 | mpMapCanvas->setMapTool(mpPanTool); 93 | 94 | } 95 | void MainWindow::zoomInMode() 96 | { 97 | mpMapCanvas->setMapTool(mpZoomInTool); 98 | } 99 | void MainWindow::zoomOutMode() 100 | { 101 | mpMapCanvas->setMapTool(mpZoomOutTool); 102 | } 103 | void MainWindow::addLayer() 104 | { 105 | QString myFileName = QFileDialog::getOpenFileName(this, tr("Open File"), 106 | QCoreApplication::applicationDirPath () + "/data", 107 | tr("GeoTiff (*.tif)")); 108 | QFileInfo myRasterFileInfo(myFileName); 109 | QgsRasterLayer * mypLayer = new QgsRasterLayer(myRasterFileInfo.filePath(), 110 | myRasterFileInfo.completeBaseName()); 111 | if (mypLayer->isValid()) 112 | { 113 | qDebug("Layer is valid"); 114 | } 115 | else 116 | { 117 | qDebug("Layer is NOT valid"); 118 | return; 119 | } 120 | 121 | // render strategy for grayscale image (will be rendered as pseudocolor) 122 | mypLayer->setDrawingStyle( QgsRasterLayer::SingleBandPseudoColor ); 123 | mypLayer->setColorShadingAlgorithm( QgsRasterLayer::PseudoColorShader ); 124 | mypLayer->setContrastEnhancementAlgorithm( 125 | QgsContrastEnhancement::StretchToMinimumMaximum, false ); 126 | mypLayer->setMinimumValue( mypLayer->grayBandName(), 0.0, false ); 127 | mypLayer->setMaximumValue( mypLayer->grayBandName(), 10.0 ); 128 | 129 | 130 | // Add the Vector Layer to the Layer Registry 131 | QgsMapLayerRegistry::instance()->addMapLayer(mypLayer, TRUE); 132 | 133 | //create a layerset 134 | QList myList; 135 | // Add the layers to the Layer Set 136 | myList.append(QgsMapCanvasLayer(mypLayer, TRUE));//bool visibility 137 | // set the canvas to the extent of our layer 138 | mpMapCanvas->setExtent(mypLayer->extent()); 139 | // Set the Map Canvas Layer Set 140 | mpMapCanvas->setLayerSet(myList); 141 | } 142 | -------------------------------------------------------------------------------- /4_adding_rasters_to_canvas/mainwindow.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * Copyright (C) 2006 by Tim Sutton * 3 | * tim@linfiniti.com * 4 | * * 5 | * This program is free software; you can redistribute it and/or modify * 6 | * it under the terms of the GNU General Public License as published by * 7 | * the Free Software Foundation; either version 2 of the License, or * 8 | * (at your option) any later version. * 9 | * * 10 | * This program is distributed in the hope that it will be useful, * 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 13 | * GNU General Public License for more details. * 14 | * * 15 | * You should have received a copy of the GNU General Public License * 16 | * along with this program; if not, write to the * 17 | * Free Software Foundation, Inc., * 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 19 | ***************************************************************************/ 20 | #ifndef OMGMAINWINDOW_H 21 | #define OMGMAINWINDOW_H 22 | 23 | //QGis Includes 24 | #include 25 | #include 26 | 27 | //QT Includes 28 | #include 29 | 30 | //Local Includes 31 | #include 32 | 33 | /** 34 | @author Tim Sutton 35 | */ 36 | class MainWindow : public QMainWindow, private Ui::MainWindowBase 37 | { 38 | Q_OBJECT; 39 | public: 40 | MainWindow(QWidget* parent = 0, Qt::WFlags fl = 0 ); 41 | ~MainWindow(); 42 | public slots: 43 | void zoomInMode(); 44 | void zoomOutMode(); 45 | void panMode(); 46 | void addLayer(); 47 | 48 | private: 49 | QgsMapCanvas * mpMapCanvas; 50 | QVBoxLayout * mpLayout; 51 | QToolBar * mpMapToolBar; 52 | QgsMapTool * mpPanTool; 53 | QgsMapTool * mpZoomInTool; 54 | QgsMapTool * mpZoomOutTool; 55 | 56 | }; 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /4_adding_rasters_to_canvas/mainwindowbase.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | MainWindowBase 6 | 7 | 8 | 9 | 0 10 | 0 11 | 579 12 | 330 13 | 14 | 15 | 16 | MainWindow 17 | 18 | 19 | 20 | 21 | 9 22 | 23 | 24 | 6 25 | 26 | 27 | 28 | 29 | QFrame::StyledPanel 30 | 31 | 32 | QFrame::Raised 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 0 42 | 0 43 | 579 44 | 22 45 | 46 | 47 | 48 | 49 | Map 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | :/mActionZoomIn.png 62 | 63 | 64 | Zoom In 65 | 66 | 67 | 68 | 69 | :/mActionZoomOut.png 70 | 71 | 72 | Zoom Out 73 | 74 | 75 | 76 | 77 | :/mActionPan.png 78 | 79 | 80 | Pan 81 | 82 | 83 | 84 | 85 | :/mActionAddLayer.png 86 | 87 | 88 | Add Layer 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | -------------------------------------------------------------------------------- /4_adding_rasters_to_canvas/resources.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | mActionAddLayer.png 4 | mActionPan.png 5 | mActionZoomIn.png 6 | mActionZoomOut.png 7 | 8 | 9 | -------------------------------------------------------------------------------- /4_adding_rasters_to_canvas/tutorial4.html: -------------------------------------------------------------------------------- 1 | In my last tutorial I showed you how to label vector features on the map canvas. Today we will take a brief look at how to add a raster map to the canvas. 2 | 3 | 4 | 5 | 6 | 7 | QGIS supports displaying more or less any raster formats that are supported by GDAL. More specifically it supports the formats that were compiled into the version of GDAL on your machine. Once a raster layer is loaded you can colourise it if its a greyscale image using a pseudocolour palette that is generated by computing the minimum and maximum values of your layer and then creating 255 equal classbreaks and assigning each break its own colour. Multiband images are also supported by QGIS and you can map individual bands to the red, green and blue (RGB) components of the image. In this tutorial I will focus only on dealing with single band, greyscale images. 8 | 9 | I used as a basis for this tutorial the project from tutorial 2 (so its worth going over that before reading this tutorial). I've replaced the vector orientated code from tutorial 2 with raster orientated equivalents. 10 | 11 |

As with my previous tutorials, the entire project can be checked out from the QGIS Subversion repository using the following command:

12 | 13 |
svn co https://svn.qgis.org/repos/qgis/trunk/code_examples/4_adding_rasters_to_canvas
14 | 
15 | 16 |

In the working directory for the tutorial code you will find a number of files including c++ sources, icons and a simple data file under data. There is also the .ui file for the main window.

17 | 18 |

Note: You will need to edit the .pro file in the above svn directory to match your system. 19 | 20 | 21 |

Raster Specifics

22 | 23 | You can browse the code for QgsRasterLayer and related classes using the QGIS source browser. Look in the 'raster' subdirectory. Also for more implementation examples of how to set up the various properties of raster layers, it is worth taking a look at 24 | qgsrasterlayerproperties.cpp. Now on with our demo application.... 25 | 26 | 27 | The first thing you should notice is that in 4_adding_rasters_to_canvas.pro I haved added -lqgis_raster to the list of LIBS to be linked to. 28 | 29 | In mainwindow.cpp you will notice a the include needed to work with rasters: 30 | 31 |
32 |     #include <qgsrasterlayer.h>
33 | 
34 | 35 | The code that follows sets up the main window and toolbars as covered in previous tutorials. The bit we are really interested in here is: 36 | 37 | 38 |
 
39 | void MainWindow::addLayer()
40 | {
41 |   QFileInfo myRasterFileInfo("data/Abarema_jupunba_projection.tif");
42 |   QgsRasterLayer * mypLayer = new QgsRasterLayer(myRasterFileInfo.filePath(), 
43 |       myRasterFileInfo.completeBaseName());
44 |   if (mypLayer->isValid())
45 |   {
46 |     qDebug("Layer is valid");
47 |   }
48 |   else
49 |   {
50 |     qDebug("Layer is NOT valid");
51 |     return; 
52 |   }
53 |   mypLayer->setColorRampingType(QgsRasterLayer::BLUE_GREEN_RED);
54 |   mypLayer->setDrawingStyle(QgsRasterLayer::SINGLE_BAND_PSEUDO_COLOR);
55 |   std::deque myLayerSet;
56 | 
57 |   // Add the Vector Layer to the Layer Registry
58 |   QgsMapLayerRegistry::instance()->addMapLayer(mypLayer, TRUE);
59 | 
60 |   // Add the Layer to the Layer Set
61 |   myLayerSet.push_back(mypLayer->getLayerID());
62 |   mypLayer->setVisible(TRUE);
63 |   // set teh canvas to the extent of our layer
64 |   mpMapCanvas->setExtent(mypLayer->extent());
65 |   // Set the Map Canvas Layer Set
66 |   mpMapCanvas->setLayerSet(myLayerSet);
67 | }
68 | 
69 | 70 | 71 | 72 | 73 | Well that wraps up this tutorial. Adding rasters to your mapcanvas is easy and doesnt take much coding. One thing you should bare in mind is that QGIS does not support on the fly reprojection of rasters so if you are planning to mix rasters from different spatial reference systesm you will probably get undesirable results. If you are planning to add vectors over the raster, add the raster layer to your canvas first and then the vectors. If the vectors are in a different spatial reference system to the raster, these vectors can be reprojected on the fly - which is something I will cover in a future tutorial. 74 | 75 |

Mac Specific Notes

76 | 77 | After building the application bundle (qmake; make) you can copy the spatial reference system database into the bundle to avoid 'cant find resource' type errors: 78 | 79 |
80 | mkdir -p qgis_example3.app/Contents/MacOS/share/qgis/resources/ 
81 | cp -r /Applications/qgis.app/Contents/MacOS/share/qgis/resources/* \ 
82 |          qgis_example3.app/Contents/MacOS/share/qgis/resources/
83 | 
84 | -------------------------------------------------------------------------------- /5_using_rubber_band_with_canvas/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.6) 2 | PROJECT(timtut5) 3 | SET(CMAKE_COLOR_MAKEFILE ON) 4 | # set path to additional CMake modules 5 | SET(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake_find_rules ${CMAKE_MODULE_PATH}) 6 | FIND_PACKAGE(QGIS REQUIRED) 7 | IF (NOT QGIS_FOUND) 8 | MESSAGE (SEND_ERROR "QGIS dependency was not found!") 9 | ENDIF (NOT QGIS_FOUND) 10 | IF (WIN32) 11 | # expect that classes are being imported by default 12 | # Note: MSVC doesn't like when the macros are quotes 13 | # and MSYS doesn't like them unqouted (bacause of braces) 14 | # import qgis classes 15 | IF (MSVC) 16 | ADD_DEFINITIONS("-DGUI_EXPORT=__declspec(dllimport)") 17 | ADD_DEFINITIONS("-DCORE_EXPORT=__declspec(dllimport)") 18 | ELSE (MSVC) 19 | ADD_DEFINITIONS("\"-DGUI_EXPORT=__declspec(dllimport)\"") 20 | ADD_DEFINITIONS("\"-DCORE_EXPORT=__declspec(dllimport)\"") 21 | ENDIF (MSVC) 22 | ELSE (WIN32) 23 | ADD_DEFINITIONS(-DGUI_EXPORT=) 24 | ADD_DEFINITIONS(-DCORE_EXPORT=) 25 | ENDIF (WIN32) 26 | 27 | FIND_PACKAGE(GDAL REQUIRED) 28 | IF (NOT GDAL_FOUND) 29 | MESSAGE (SEND_ERROR "GDAL dependency was not found!") 30 | ENDIF (NOT GDAL_FOUND) 31 | 32 | FIND_PACKAGE(GEOS REQUIRED) 33 | IF (NOT GEOS_FOUND) 34 | MESSAGE (SEND_ERROR "GEOS dependency was not found!") 35 | ENDIF (NOT GEOS_FOUND) 36 | 37 | IF (CMAKE_BUILD_TYPE MATCHES Debug) 38 | ADD_DEFINITIONS(-Dtimtut5DEBUG=1) 39 | ENDIF (CMAKE_BUILD_TYPE MATCHES Debug) 40 | ######################################################## 41 | # Files 42 | 43 | SET (timtut5_SRCS 44 | main.cpp 45 | mainwindow.cpp 46 | ) 47 | 48 | # This tut uses no UIs 49 | SET (timtut5_UIS 50 | mainwindowbase.ui 51 | ) 52 | 53 | # This tut needs no MOC 54 | SET (timtut5_MOC_HDRS 55 | mainwindow.h 56 | ) 57 | 58 | 59 | SET (timtut5_RCCS 60 | resources.qrc 61 | ) 62 | 63 | SET (QT_USE_QT3SUPPORT FALSE) 64 | SET (QT_USE_QTGUI TRUE) 65 | SET (QT_USE_QTSQL TRUE) 66 | SET (QT_USE_QTSVG TRUE) 67 | SET (QT_USE_QTXML TRUE) 68 | SET (QT_USE_QTNETWORK TRUE) 69 | FIND_PACKAGE(Qt4 REQUIRED) 70 | INCLUDE( ${QT_USE_FILE} ) 71 | ######################################################## 72 | # Build 73 | 74 | QT4_WRAP_UI (timtut5_UIS_H ${timtut5_UIS}) 75 | 76 | QT4_WRAP_CPP (timtut5_MOC_SRCS ${timtut5_MOC_HDRS}) 77 | 78 | QT4_ADD_RESOURCES(timtut5_RCC_SRCS ${timtut5_RCCS}) 79 | 80 | ADD_EXECUTABLE (timtut5 ${timtut5_SRCS} ${timtut5_MOC_SRCS} ${timtut5_RCC_SRCS} ${timtut5_UIS_H}) 81 | 82 | INCLUDE_DIRECTORIES( 83 | ${GDAL_INCLUDE_DIR} 84 | ${GEOS_INCLUDE_DIR} 85 | ${CMAKE_CURRENT_BINARY_DIR} 86 | ${QT_INCLUDE_DIR} 87 | ${QGIS_INCLUDE_DIR} 88 | . 89 | ) 90 | 91 | #This is probably no longer needed, but I will leave it in for Win machines for the moment 92 | IF(QT_QTSQL_FOUND) 93 | FIND_LIBRARY(QT_QTSQL_LIBRARY NAMES QtSql QtSql5 PATHS ${QT_LIBRARY_DIR} NO_DEFAULT_PATH) 94 | SET(QT_LIBRARIES ${QT_LIBRARIES} ${QT_QTSQL_LIBRARY}) 95 | ENDIF(QT_QTSQL_FOUND) 96 | 97 | TARGET_LINK_LIBRARIES(timtut5 98 | ${QT_LIBRARIES} 99 | ${QGIS_CORE_LIBRARY} 100 | ${QGIS_GUI_LIBRARY} 101 | ${GEOS_LIBRARY} 102 | ${GDAL_LIBRARY} 103 | ) 104 | 105 | IF (MSVC) 106 | # Very important or you get all kinds of odd side 107 | #effects like app crash on start up saying qtgui.dll 108 | TARGET_LINK_LIBRARIES( timtut5 109 | qtmain 110 | ) 111 | ENDIF (MSVC) 112 | 113 | ######################################################## 114 | # Install 115 | 116 | 117 | -------------------------------------------------------------------------------- /5_using_rubber_band_with_canvas/cmake_find_rules/FindGDAL.cmake: -------------------------------------------------------------------------------- 1 | ## Once run this will define: 2 | ## 3 | ## GDAL_FOUND = system has GDAL lib 4 | ## 5 | ## GDAL_LIBRARY = full path to the library 6 | ## 7 | ## GDAL_INCLUDE_DIR = where to find headers 8 | ## 9 | ## Magnus Homann 10 | 11 | 12 | IF(WIN32) 13 | 14 | IF (MINGW) 15 | FIND_PATH(GDAL_INCLUDE_DIR gdal.h /usr/local/include /usr/include c:/msys/local/include) 16 | FIND_LIBRARY(GDAL_LIBRARY NAMES gdal PATHS /usr/local/lib /usr/lib c:/msys/local/lib) 17 | ENDIF (MINGW) 18 | 19 | IF (MSVC) 20 | FIND_PATH(GDAL_INCLUDE_DIR gdal.h 21 | "$ENV{LIB_DIR}/include/gdal" 22 | ) 23 | FIND_LIBRARY(GDAL_LIBRARY NAMES gdal PATHS 24 | "$ENV{LIB_DIR}/lib/" 25 | ) 26 | 27 | # NOTE: under msvc you should add the following to your target link libraries 28 | # list as they are required by gdal 29 | #odbc32 odbccp32 30 | 31 | ENDIF (MSVC) 32 | 33 | 34 | ELSE(WIN32) 35 | IF(UNIX) 36 | 37 | # try to use framework on mac 38 | IF (APPLE) 39 | SET (GDAL_MAC_PATH /Library/Frameworks/GDAL.framework/unix/bin) 40 | ENDIF (APPLE) 41 | 42 | SET(GDAL_CONFIG_PREFER_PATH "$ENV{GDAL_HOME}/bin" CACHE STRING "preferred path to GDAL (gdal-config)") 43 | FIND_PROGRAM(GDAL_CONFIG gdal-config 44 | ${GDAL_CONFIG_PREFER_PATH} 45 | ${GDAL_MAC_PATH} 46 | /usr/local/bin/ 47 | /usr/bin/ 48 | ) 49 | # MESSAGE("DBG GDAL_CONFIG ${GDAL_CONFIG}") 50 | 51 | IF (GDAL_CONFIG) 52 | # set INCLUDE_DIR to prefix+include 53 | EXEC_PROGRAM(${GDAL_CONFIG} 54 | ARGS --prefix 55 | OUTPUT_VARIABLE GDAL_PREFIX) 56 | #SET(GDAL_INCLUDE_DIR ${GDAL_PREFIX}/include CACHE STRING INTERNAL) 57 | FIND_PATH(GDAL_INCLUDE_DIR 58 | gdal.h 59 | ${GDAL_PREFIX}/include/gdal 60 | ${GDAL_PREFIX}/include 61 | /usr/local/include 62 | /usr/include 63 | ) 64 | 65 | ## extract link dirs for rpath 66 | EXEC_PROGRAM(${GDAL_CONFIG} 67 | ARGS --libs 68 | OUTPUT_VARIABLE GDAL_CONFIG_LIBS ) 69 | 70 | ## split off the link dirs (for rpath) 71 | ## use regular expression to match wildcard equivalent "-L*" 72 | ## with is a space or a semicolon 73 | STRING(REGEX MATCHALL "[-][L]([^ ;])+" 74 | GDAL_LINK_DIRECTORIES_WITH_PREFIX 75 | "${GDAL_CONFIG_LIBS}" ) 76 | # MESSAGE("DBG GDAL_LINK_DIRECTORIES_WITH_PREFIX=${GDAL_LINK_DIRECTORIES_WITH_PREFIX}") 77 | 78 | ## remove prefix -L because we need the pure directory for LINK_DIRECTORIES 79 | 80 | IF (GDAL_LINK_DIRECTORIES_WITH_PREFIX) 81 | STRING(REGEX REPLACE "[-][L]" "" GDAL_LINK_DIRECTORIES ${GDAL_LINK_DIRECTORIES_WITH_PREFIX} ) 82 | ENDIF (GDAL_LINK_DIRECTORIES_WITH_PREFIX) 83 | 84 | ## split off the name 85 | ## use regular expression to match wildcard equivalent "-l*" 86 | ## with is a space or a semicolon 87 | STRING(REGEX MATCHALL "[-][l]([^ ;])+" 88 | GDAL_LIB_NAME_WITH_PREFIX 89 | "${GDAL_CONFIG_LIBS}" ) 90 | # MESSAGE("DBG GDAL_LIB_NAME_WITH_PREFIX=${GDAL_LIB_NAME_WITH_PREFIX}") 91 | 92 | 93 | ## remove prefix -l because we need the pure name 94 | 95 | IF (GDAL_LIB_NAME_WITH_PREFIX) 96 | STRING(REGEX REPLACE "[-][l]" "" GDAL_LIB_NAME ${GDAL_LIB_NAME_WITH_PREFIX} ) 97 | ENDIF (GDAL_LIB_NAME_WITH_PREFIX) 98 | 99 | IF (APPLE) 100 | SET(GDAL_LIBRARY ${GDAL_LINK_DIRECTORIES}/lib${GDAL_LIB_NAME}.dylib CACHE STRING INTERNAL) 101 | ELSE (APPLE) 102 | SET(GDAL_LIBRARY ${GDAL_LINK_DIRECTORIES}/lib${GDAL_LIB_NAME}.so CACHE STRING INTERNAL) 103 | ENDIF (APPLE) 104 | 105 | ELSE(GDAL_CONFIG) 106 | MESSAGE("FindGDAL.cmake: gdal-config not found. Please set it manually. GDAL_CONFIG=${GDAL_CONFIG}") 107 | ENDIF(GDAL_CONFIG) 108 | 109 | ENDIF(UNIX) 110 | ENDIF(WIN32) 111 | 112 | 113 | IF (GDAL_INCLUDE_DIR AND GDAL_LIBRARY) 114 | SET(GDAL_FOUND TRUE) 115 | ENDIF (GDAL_INCLUDE_DIR AND GDAL_LIBRARY) 116 | 117 | IF (GDAL_FOUND) 118 | 119 | IF (NOT GDAL_FIND_QUIETLY) 120 | MESSAGE(STATUS "Found GDAL: ${GDAL_LIBRARY}") 121 | ENDIF (NOT GDAL_FIND_QUIETLY) 122 | 123 | ELSE (GDAL_FOUND) 124 | 125 | MESSAGE(GDAL_INCLUDE_DIR=${GDAL_INCLUDE_DIR}) 126 | MESSAGE(GDAL_LIBRARY=${GDAL_LIBRARY}) 127 | MESSAGE(FATAL_ERROR "Could not find GDAL") 128 | 129 | ENDIF (GDAL_FOUND) 130 | -------------------------------------------------------------------------------- /5_using_rubber_band_with_canvas/cmake_find_rules/FindGEOS.cmake: -------------------------------------------------------------------------------- 1 | 2 | # CMake module to search for GEOS library 3 | # 4 | # If it's found it sets GEOS_FOUND to TRUE 5 | # and following variables are set: 6 | # GEOS_INCLUDE_DIR 7 | # GEOS_LIBRARY 8 | 9 | 10 | FIND_PATH(GEOS_INCLUDE_DIR geos.h 11 | /usr/local/include 12 | /usr/include 13 | #MSVC 14 | "$ENV{LIB_DIR}/include" 15 | #mingw 16 | c:/msys/local/include 17 | ) 18 | 19 | FIND_LIBRARY(GEOS_LIBRARY NAMES geos PATHS 20 | /usr/local/lib 21 | /usr/lib 22 | #MSVC 23 | "$ENV{LIB_DIR}/lib" 24 | #mingw 25 | c:/msys/local/lib 26 | ) 27 | 28 | IF (GEOS_INCLUDE_DIR AND GEOS_LIBRARY) 29 | SET(GEOS_FOUND TRUE) 30 | ENDIF (GEOS_INCLUDE_DIR AND GEOS_LIBRARY) 31 | 32 | 33 | IF (GEOS_FOUND) 34 | 35 | IF (NOT GEOS_FIND_QUIETLY) 36 | MESSAGE(STATUS "Found GEOS: ${GEOS_LIBRARY}") 37 | ENDIF (NOT GEOS_FIND_QUIETLY) 38 | 39 | ELSE (GEOS_FOUND) 40 | 41 | IF (GEOS_FIND_REQUIRED) 42 | MESSAGE(FATAL_ERROR "Could not find GEOS") 43 | ENDIF (GEOS_FIND_REQUIRED) 44 | 45 | ENDIF (GEOS_FOUND) 46 | -------------------------------------------------------------------------------- /5_using_rubber_band_with_canvas/cmake_find_rules/FindQGIS.cmake: -------------------------------------------------------------------------------- 1 | ## Once run this will define: 2 | ## 3 | ## QGIS_FOUND = system has QGIS lib 4 | ## 5 | ## QGIS_CORE_LIBRARY = full path to the CORE library 6 | ## QGIS_GUI_LIBRARY = full path to the GUI library 7 | ## QGIS_PLUGIN_DIR = full path to where QGIS plugins are installed 8 | ## QGIS_INCLUDE_DIR = where to find headers 9 | ## 10 | ## Tim Sutton 11 | 12 | #MESSAGE("Searching for QGIS") 13 | IF(WIN32) 14 | #MESSAGE("Searching for QGIS in C:/program files/Quantum GIS") 15 | IF (MINGW) 16 | FIND_PATH(QGIS_PLUGIN_DIR libnortharrowplugin.dll 17 | "C:/Program Files/Quantum GIS/plugins" 18 | ) 19 | FIND_PATH(QGIS_INCLUDE_DIR qgsapplication.h 20 | "C:/Program Files/Quantum GIS/include" 21 | ) 22 | FIND_LIBRARY(QGIS_CORE_LIBRARY NAMES qgis_core PATHS 23 | "C:/Program Files/Quantum GIS/" 24 | ) 25 | FIND_LIBRARY(QGIS_GUI_LIBRARY NAMES qgis_gui PATHS 26 | "C:/Program Files/Quantum GIS/" 27 | ) 28 | ENDIF (MINGW) 29 | 30 | IF (MSVC) 31 | FIND_PATH(QGIS_PLUGIN_DIR libnortharrowplugin.dll 32 | "C:/Program Files/Quantum GIS/lib/qgis" 33 | ) 34 | FIND_PATH(QGIS_INCLUDE_DIR qgsapplication.h 35 | "$ENV{LIB_DIR}/include/qgis" 36 | ) 37 | FIND_LIBRARY(QGIS_CORE_LIBRARY NAMES qgis_core PATHS 38 | "$ENV{LIB_DIR}/lib/" 39 | ) 40 | FIND_LIBRARY(QGIS_GUI_LIBRARY NAMES qgis_gui PATHS 41 | "$ENV{LIB_DIR}/lib/" 42 | ) 43 | ENDIF (MSVC) 44 | 45 | ELSE(WIN32) 46 | IF(UNIX) 47 | 48 | # try to use bundle on mac 49 | IF (APPLE) 50 | #MESSAGE("Searching for QGIS in /Applications/QGIS.app/Contents/MacOS") 51 | SET (QGIS_MAC_PATH /Applications/QGIS1.0.0.app/Contents/MacOS) 52 | SET (QGIS_LIB_DIR ${QGIS_MAC_PATH}/lib) 53 | SET (QGIS_PLUGIN_DIR ${QGIS_MAC_PATH}/lib/qgis CACHE STRING INTERNAL) 54 | # set INCLUDE_DIR to prefix+include 55 | SET(QGIS_INCLUDE_DIR ${QGIS_MAC_PATH}/include/qgis CACHE STRING INTERNAL) 56 | ## extract link dirs 57 | SET(QGIS_CORE_LIBRARY ${QGIS_LIB_DIR}/libqgis_core.dylib CACHE STRING INTERNAL) 58 | SET(QGIS_GUI_LIBRARY ${QGIS_LIB_DIR}/libqgis_gui.dylib CACHE STRING INTERNAL) 59 | ELSE (APPLE) # Unix / Linux 60 | #MESSAGE("Searching for QGIS in /usr/bin; /usr/local/bin") 61 | FIND_PATH(QGIS_PLUGIN_DIR libnortharrowplugin.so 62 | /usr/lib/qgis 63 | /usr/local/lib/qgis 64 | "$ENV{LIB_DIR}/lib/qgis" 65 | ) 66 | FIND_PATH(QGIS_INCLUDE_DIR qgis.h 67 | /usr/include/qgis 68 | /usr/local/include/qgis 69 | "$ENV{LIB_DIR}/include/qgis" 70 | ) 71 | FIND_LIBRARY(QGIS_CORE_LIBRARY NAMES qgis_core PATHS 72 | /usr/lib 73 | /usr/local/lib 74 | "$ENV{LIB_DIR}/lib" 75 | ) 76 | FIND_LIBRARY(QGIS_GUI_LIBRARY NAMES qgis_gui PATHS 77 | /usr/lib 78 | /usr/local/lib 79 | "$ENV{LIB_DIR}/lib" 80 | ) 81 | ENDIF (APPLE) 82 | ENDIF(UNIX) 83 | ENDIF(WIN32) 84 | 85 | 86 | IF (QGIS_INCLUDE_DIR AND QGIS_CORE_LIBRARY AND QGIS_GUI_LIBRARY) 87 | SET(QGIS_FOUND TRUE) 88 | ENDIF (QGIS_INCLUDE_DIR AND QGIS_CORE_LIBRARY AND QGIS_GUI_LIBRARY) 89 | 90 | IF (QGIS_FOUND) 91 | IF (NOT QGIS_FIND_QUIETLY) 92 | MESSAGE(STATUS "Found QGIS Core: ${QGIS_CORE_LIBRARY}") 93 | MESSAGE(STATUS "Found QGIS Gui: ${QGIS_GUI_LIBRARY}") 94 | MESSAGE(STATUS "Found QGIS Plugins Dir: ${QGIS_PLUGIN_DIR}") 95 | ENDIF (NOT QGIS_FIND_QUIETLY) 96 | ELSE (QGIS_FOUND) 97 | IF (QGIS_FIND_REQUIRED) 98 | MESSAGE(FATAL_ERROR "Could not find QGIS") 99 | ENDIF (QGIS_FIND_REQUIRED) 100 | ENDIF (QGIS_FOUND) 101 | -------------------------------------------------------------------------------- /5_using_rubber_band_with_canvas/data/Abarema_jupunba_projection.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/5_using_rubber_band_with_canvas/data/Abarema_jupunba_projection.tif -------------------------------------------------------------------------------- /5_using_rubber_band_with_canvas/mActionAddLayer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/5_using_rubber_band_with_canvas/mActionAddLayer.png -------------------------------------------------------------------------------- /5_using_rubber_band_with_canvas/mActionPan.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/5_using_rubber_band_with_canvas/mActionPan.png -------------------------------------------------------------------------------- /5_using_rubber_band_with_canvas/mActionZoomIn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/5_using_rubber_band_with_canvas/mActionZoomIn.png -------------------------------------------------------------------------------- /5_using_rubber_band_with_canvas/mActionZoomOut.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/5_using_rubber_band_with_canvas/mActionZoomOut.png -------------------------------------------------------------------------------- /5_using_rubber_band_with_canvas/main.cpp: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * Copyright (C) 2006 by Tim Sutton * 3 | * tim@linfiniti.com * 4 | * * 5 | * This program is free software; you can redistribute it and/or modify * 6 | * it under the terms of the GNU General Public License as published by * 7 | * the Free Software Foundation; either version 2 of the License, or * 8 | * (at your option) any later version. * 9 | * * 10 | * This program is distributed in the hope that it will be useful, * 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 13 | * GNU General Public License for more details. * 14 | * * 15 | * You should have received a copy of the GNU General Public License * 16 | * along with this program; if not, write to the * 17 | * Free Software Foundation, Inc., * 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 19 | ***************************************************************************/ 20 | // 21 | // Qt Includes 22 | // 23 | #include 24 | #include 25 | // 26 | // QGIS Includes 27 | // 28 | #include 29 | 30 | int main(int argc, char ** argv) 31 | { 32 | // Start the Application 33 | QgsApplication app(argc, argv, TRUE); 34 | MainWindow * mypMainWindow = new MainWindow(); 35 | mypMainWindow->show(); 36 | // Start the Application Event Loop 37 | return app.exec(); 38 | } 39 | -------------------------------------------------------------------------------- /5_using_rubber_band_with_canvas/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * Copyright (C) 2006 by Tim Sutton * 3 | * tim@linfiniti.com * 4 | * * 5 | * This program is free software; you can redistribute it and/or modify * 6 | * it under the terms of the GNU General Public License as published by * 7 | * the Free Software Foundation; either version 2 of the License, or * 8 | * (at your option) any later version. * 9 | * * 10 | * This program is distributed in the hope that it will be useful, * 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 13 | * GNU General Public License for more details. * 14 | * * 15 | * You should have received a copy of the GNU General Public License * 16 | * along with this program; if not, write to the * 17 | * Free Software Foundation, Inc., * 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 19 | ***************************************************************************/ 20 | #include "mainwindow.h" 21 | // 22 | // QGIS Includes 23 | // 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | // 31 | // Needed fr rubber band support 32 | // 33 | #include 34 | // 35 | // QGIS Map tools 36 | // 37 | #include "qgsmaptoolpan.h" 38 | #include "qgsmaptoolzoom.h" 39 | 40 | MainWindow::MainWindow(QWidget* parent, Qt::WFlags fl) 41 | : QMainWindow(parent,fl) 42 | { 43 | //required by Qt4 to initialise the ui 44 | setupUi(this); 45 | 46 | // Instantiate Provider Registry 47 | #if defined(Q_WS_MAC) 48 | QString myPluginsDir = "/Users/timsutton/apps/qgis.app/Contents/MacOS/lib/qgis"; 49 | #else 50 | QString myPluginsDir = "/home/timlinux/apps/lib/qgis"; 51 | #endif 52 | QgsProviderRegistry::instance(myPluginsDir); 53 | 54 | 55 | // Create the Map Canvas 56 | mpMapCanvas= new QgsMapCanvas(0, 0); 57 | mpMapCanvas->enableAntiAliasing(true); 58 | mpMapCanvas->setCanvasColor(QColor(255, 255, 255)); 59 | mpMapCanvas->freeze(false); 60 | mpMapCanvas->setVisible(true); 61 | mpMapCanvas->refresh(); 62 | mpMapCanvas->show(); 63 | 64 | // Lay our widgets out in the main window 65 | mpLayout = new QVBoxLayout(frameMap); 66 | mpLayout->addWidget(mpMapCanvas); 67 | 68 | //create the action behaviours 69 | connect(mpActionPan, SIGNAL(triggered()), this, SLOT(panMode())); 70 | connect(mpActionZoomIn, SIGNAL(triggered()), this, SLOT(zoomInMode())); 71 | connect(mpActionZoomOut, SIGNAL(triggered()), this, SLOT(zoomOutMode())); 72 | connect(mpActionAddLayer, SIGNAL(triggered()), this, SLOT(addLayer())); 73 | 74 | //create a little toolbar 75 | mpMapToolBar = addToolBar(tr("File")); 76 | mpMapToolBar->addAction(mpActionAddLayer); 77 | mpMapToolBar->addAction(mpActionZoomIn); 78 | mpMapToolBar->addAction(mpActionZoomOut); 79 | mpMapToolBar->addAction(mpActionPan); 80 | 81 | //create the maptools 82 | mpPanTool = new QgsMapToolPan(mpMapCanvas); 83 | mpPanTool->setAction(mpActionPan); 84 | mpZoomInTool = new QgsMapToolZoom(mpMapCanvas, FALSE); // false = in 85 | mpZoomInTool->setAction(mpActionZoomIn); 86 | mpZoomOutTool = new QgsMapToolZoom(mpMapCanvas, TRUE ); //true = out 87 | mpZoomOutTool->setAction(mpActionZoomOut); 88 | 89 | //create the rubber band 90 | bool myPolygonFlag=true; 91 | mpRubberBand = new QgsRubberBand(mpMapCanvas, myPolygonFlag ); 92 | mpRubberBand->show(); 93 | } 94 | 95 | MainWindow::~MainWindow() 96 | { 97 | delete mpZoomInTool; 98 | delete mpZoomOutTool; 99 | delete mpPanTool; 100 | delete mpMapToolBar; 101 | delete mpMapCanvas; 102 | delete mpLayout; 103 | delete mpRubberBand; 104 | } 105 | 106 | void MainWindow::panMode() 107 | { 108 | mpMapCanvas->setMapTool(mpPanTool); 109 | 110 | } 111 | void MainWindow::zoomInMode() 112 | { 113 | mpMapCanvas->setMapTool(mpZoomInTool); 114 | } 115 | void MainWindow::zoomOutMode() 116 | { 117 | mpMapCanvas->setMapTool(mpZoomOutTool); 118 | } 119 | void MainWindow::addLayer() 120 | { 121 | QFileInfo myRasterFileInfo("../data/Abarema_jupunba_projection.tif"); 122 | QgsRasterLayer * mypLayer = new QgsRasterLayer(myRasterFileInfo.filePath(), 123 | myRasterFileInfo.completeBaseName()); 124 | if (mypLayer->isValid()) 125 | { 126 | qDebug("Layer is valid"); 127 | } 128 | else 129 | { 130 | qDebug("Layer is NOT valid"); 131 | return; 132 | } 133 | // render strategy for grayscale image (will be rendered as pseudocolor) 134 | mypLayer->setDrawingStyle( QgsRasterLayer::SingleBandPseudoColor ); 135 | mypLayer->setColorShadingAlgorithm( QgsRasterLayer::PseudoColorShader ); 136 | mypLayer->setContrastEnhancementAlgorithm( 137 | QgsContrastEnhancement::StretchToMinimumMaximum, false ); 138 | mypLayer->setMinimumValue( mypLayer->grayBandName(), 0.0, false ); 139 | mypLayer->setMaximumValue( mypLayer->grayBandName(), 10.0 ); 140 | //create a layerset 141 | QList myList; 142 | // Add the layers to the Layer Set 143 | myList.append(QgsMapCanvasLayer(mypLayer, TRUE));//bool visibility 144 | // set the canvas to the extent of our layer 145 | mpMapCanvas->setExtent(mypLayer->extent()); 146 | 147 | // Add the Vector Layer to the Layer Registry 148 | QgsMapLayerRegistry::instance()->addMapLayer(mypLayer, TRUE); 149 | 150 | // Set the Map Canvas Layer Set 151 | mpMapCanvas->setLayerSet(myList); 152 | } 153 | void MainWindow::on_mpToolShowRubberBand_clicked() 154 | { 155 | QgsPoint myPoint1 = mpMapCanvas->getCoordinateTransform()->toMapCoordinates(10, 10); 156 | mpRubberBand->addPoint(myPoint1); 157 | QgsPoint myPoint2 = mpMapCanvas->getCoordinateTransform()->toMapCoordinates(20, 10); 158 | mpRubberBand->addPoint(myPoint2); 159 | QgsPoint myPoint3 = mpMapCanvas->getCoordinateTransform()->toMapCoordinates(20, 20); 160 | mpRubberBand->addPoint(myPoint3); 161 | QgsPoint myPoint4 = mpMapCanvas->getCoordinateTransform()->toMapCoordinates(10, 20); 162 | mpRubberBand->addPoint(myPoint4); 163 | } 164 | void MainWindow::on_mpToolHideRubberBand_clicked() 165 | { 166 | bool myPolygonFlag=true; 167 | mpRubberBand->reset(myPolygonFlag); 168 | } 169 | -------------------------------------------------------------------------------- /5_using_rubber_band_with_canvas/mainwindow.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * Copyright (C) 2006 by Tim Sutton * 3 | * tim@linfiniti.com * 4 | * * 5 | * This program is free software; you can redistribute it and/or modify * 6 | * it under the terms of the GNU General Public License as published by * 7 | * the Free Software Foundation; either version 2 of the License, or * 8 | * (at your option) any later version. * 9 | * * 10 | * This program is distributed in the hope that it will be useful, * 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 13 | * GNU General Public License for more details. * 14 | * * 15 | * You should have received a copy of the GNU General Public License * 16 | * along with this program; if not, write to the * 17 | * Free Software Foundation, Inc., * 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 19 | ***************************************************************************/ 20 | #ifndef OMGMAINWINDOW_H 21 | #define OMGMAINWINDOW_H 22 | 23 | //QGis Includes 24 | #include 25 | #include 26 | // 27 | // Needed for rubber band support 28 | // 29 | #include 30 | 31 | //QT Includes 32 | #include 33 | 34 | //Local Includes 35 | #include 36 | 37 | /** 38 | @author Tim Sutton 39 | */ 40 | class MainWindow : public QMainWindow, private Ui::MainWindowBase 41 | { 42 | Q_OBJECT; 43 | public: 44 | MainWindow(QWidget* parent = 0, Qt::WFlags fl = 0 ); 45 | ~MainWindow(); 46 | public slots: 47 | void zoomInMode(); 48 | void zoomOutMode(); 49 | void panMode(); 50 | void addLayer(); 51 | // next are tools overloaded from base class 52 | void on_mpToolShowRubberBand_clicked(); 53 | void on_mpToolHideRubberBand_clicked(); 54 | 55 | private: 56 | QgsMapCanvas * mpMapCanvas; 57 | QVBoxLayout * mpLayout; 58 | QToolBar * mpMapToolBar; 59 | QgsMapTool * mpPanTool; 60 | QgsMapTool * mpZoomInTool; 61 | QgsMapTool * mpZoomOutTool; 62 | QgsRubberBand * mpRubberBand; 63 | }; 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /5_using_rubber_band_with_canvas/mainwindowbase.ui: -------------------------------------------------------------------------------- 1 | 2 | MainWindowBase 3 | 4 | 5 | 6 | 0 7 | 0 8 | 579 9 | 330 10 | 11 | 12 | 13 | MainWindow 14 | 15 | 16 | 17 | 18 | 9 19 | 20 | 21 | 6 22 | 23 | 24 | 25 | 26 | QFrame::StyledPanel 27 | 28 | 29 | QFrame::Raised 30 | 31 | 32 | 33 | 34 | 35 | 36 | Rubber Band Visibility: 37 | 38 | 39 | 40 | 41 | 42 | 43 | Show 44 | 45 | 46 | 47 | 48 | 49 | 50 | Hide 51 | 52 | 53 | 54 | 55 | 56 | 57 | Qt::Horizontal 58 | 59 | 60 | 61 | 40 62 | 20 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 0 73 | 0 74 | 579 75 | 22 76 | 77 | 78 | 79 | 80 | Map 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | :/mActionZoomIn.png 93 | 94 | 95 | Zoom In 96 | 97 | 98 | 99 | 100 | :/mActionZoomOut.png 101 | 102 | 103 | Zoom Out 104 | 105 | 106 | 107 | 108 | :/mActionPan.png 109 | 110 | 111 | Pan 112 | 113 | 114 | 115 | 116 | :/mActionAddLayer.png 117 | 118 | 119 | Add Layer 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | -------------------------------------------------------------------------------- /5_using_rubber_band_with_canvas/resources.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | mActionAddLayer.png 4 | mActionPan.png 5 | mActionZoomIn.png 6 | mActionZoomOut.png 7 | 8 | 9 | -------------------------------------------------------------------------------- /5_using_rubber_band_with_canvas/tutorial5.html: -------------------------------------------------------------------------------- 1 | In my last tutorial I showed you how to add a raster layer to the map canvas. Today we will take a first look at managing transient objects on the map canvas. The QgsRubberBand class lets you draw lines and polygons on top of the map canvas, without them belonging to any data layer. This is typically useful for digitising and annotating maps. In a future tutorial I will show you how to do the same thing with point features. 2 | 3 | First lets take a sneak preview of what we will achieve in this tutorial. Heres a 'before' shot: 4 | 5 | 6 | And here is the same scene with a rubber band drawn in the top left hand corner. 7 | 8 | 9 | 10 | 11 | 12 | I used as a basis for this tutorial the project from tutorial 4 (so its worth going over that before reading this tutorial). I also suggest looking at the other tutorials before going through this one so you can get a but more familiar with the canvas API - I wont be rehashing that here. 13 | 14 |

As with my previous tutorials, the entire project can be checked out from the QGIS Subversion repository using the following command:

15 | 16 |
svn co https://svn.qgis.org/repos/qgis/trunk/code_examples/5_using_rubber_band_with_canvas
 17 | 
18 | 19 |

In the working directory for the tutorial code you will find a number of files including c++ sources, icons and a simple data file under data. There is also the .ui file for the main window.

20 | 21 |

Note: You will need to edit the .pro file in the above svn directory to match your system. 22 | 23 | 24 |

Rubber Band Specifics

25 | 26 | You can browse the code for QgsRasterLayer and related classes using the QGIS source browser. Look in the 'core' and 'gui' subdirectories. The classes we are particularly interested in are QgsRubberBand, QgsPoint and QgsMapCanvas. Also for more implementation examples, take a look at 27 | qgsmeasure.cpp. Now on with our demo application.... 28 | 29 | 30 | In mainwindow.h you will notice the include needed to work with rubber bands: 31 | 32 |
 33 |     #include <qgsrubberband.h>
 34 | 
35 | 36 | Next I added a couple of slots to handle events from the show and hide rubber band tool buttons that I added to the main window: 37 | 38 |
 39 |   // next are tools overloaded from base class
 40 |   void on_mpToolShowRubberBand_clicked();
 41 |   void on_mpToolHideRubberBand_clicked();
 42 | 
43 | 44 | Also I've added a class member for our rubber band: 45 | 46 |
 47 |   QgsRubberBand * mpRubberBand;
 48 | 
49 | 50 | All pretty straightforward so far. Lets go over to the implementation file, mainwindow.cpp. First you will see a new include: 51 | 52 |
 53 |     #include <qgspoint.h>
 54 | 
55 | 56 | This is needed because we will be using QgsPoint objects to define the vertices of our rubber band. The code that follows sets up the main window and toolbars as covered in previous tutorials. Towards the end of the constructor you will see we initialise the rubber band member: 57 | 58 |
 
 59 |      //create the rubber band
 60 |      bool myPolygonFlag=true;
 61 |      mpRubberBand = new QgsRubberBand(mpMapCanvas, myPolygonFlag );
 62 |      mpRubberBand->show();
 63 | 
64 | 65 | I've written it a bit more verbosely than necessary to make it clear. The rubber band constructor needs two parameters; the QgsMapCanvas on to which our rubber band will be drawn, and a boolean indicating whether the rubber band is a polygon or not. In the case that the rubber band is a polygon it will always close the gap between first and last points. 66 | 67 | Next I wired up the two toolButtons I added using Qt4 Designer to the mainwindowbase.ui file. Lets look at the show event first: 68 | 69 |
 70 | void MainWindow::on_mpToolShowRubberBand_clicked()
 71 | {
 72 |   QgsPoint myPoint1 = mpMapCanvas->getCoordinateTransform()->toMapCoordinates(10, 10);
 73 |   mpRubberBand->addPoint(myPoint1);
 74 |   QgsPoint myPoint2 = mpMapCanvas->getCoordinateTransform()->toMapCoordinates(20, 10);
 75 |   mpRubberBand->addPoint(myPoint2);
 76 |   QgsPoint myPoint3 = mpMapCanvas->getCoordinateTransform()->toMapCoordinates(20, 20);
 77 |   mpRubberBand->addPoint(myPoint3);
 78 |   QgsPoint myPoint4 = mpMapCanvas->getCoordinateTransform()->toMapCoordinates(10, 20);
 79 |   mpRubberBand->addPoint(myPoint4);
 80 | }
 81 | 
82 | 83 | We are specifying here to draw a 10x10 pixel box, starting its top left corner at 10,10 pixels from the corner of the map canvase. So whats all that coordinate transform stuff about then? 84 | 85 | The rubber band operates in the spatial reference system of the associated canvas. In other words when you specify the coordinates of vertices it should be in map units, not in screen pixel coordinates. Fortunately there is an easy way to translate from screen coordinates to map coordinates. First we get the 86 | QgsMapToPixel instance from the map canvas. Next we call its toMapCoordinates(int,int) method which will return a QgsPoint. For each point I create I am just adding it to the rubber band. Because I am walking around the corners of a square, the end result will be a square drawn on screen. Remember that we specified our rubber band is a polygon in the constructor, so there is no need to create a 5th point to close the polygon - its automaticall closed for us! 87 | 88 | Now that we know how to create a QgsRubberBand, we should also show how to remove it from the canvas again. This is illustrated in the next method: 89 | 90 |
 91 | void MainWindow::on_mpToolHideRubberBand_clicked()
 92 | {
 93 |   bool myPolygonFlag=true;
 94 |   mpRubberBand->reset(myPolygonFlag);
 95 | }
 96 | 
97 | 98 | Ok so thats pretty easy - just call reset on the rubber band. When you do so you need to respecify whether the rubber band is to operate in polyline or polygon mode. 99 | 100 | Lastly you should note that you are responsible for managing the memory allocated to the rubber band, this you will see that in the destructor for mainwindow.cpp I have the following: 101 | 102 |
103 | delete mpRubberBand;
104 | 
105 | 106 | You are not limited to only one QgsRubberBand instance either - you can add as many as you like. In the screenshot below I added a second rubber band: 107 | 108 | 109 | 110 | Well I hope you find lots of cool uses for QgsRubberBand - feel free to let us know in the comments below how you are using it. Thats the end of tutorial5! 111 | 112 | 113 |

Mac Specific Notes

114 | 115 | After building the application bundle (qmake; make) you can copy the spatial reference system database into the bundle to avoid 'cant find resource' type errors: 116 | 117 |
118 | mkdir -p qgis_example3.app/Contents/MacOS/share/qgis/resources/ 
119 | cp -r /Applications/qgis.app/Contents/MacOS/share/qgis/resources/* \ 
120 |          qgis_example3.app/Contents/MacOS/share/qgis/resources/
121 | 
122 | -------------------------------------------------------------------------------- /6_accessing_vector_attributes/6_accessing_vector_attributes.pro: -------------------------------------------------------------------------------- 1 | TEMPLATE = app 2 | TARGET = qgis_example6 3 | QT = qt3support sql opengl network svg gui core xml 4 | LANGUAGE= C++ 5 | linux-g++{ 6 | QGISDIR=[path to installed qgis] 7 | QGISLIBDIR=$${QGISDIR}/lib 8 | QGISSRCDIR=[path to qgis src directory] 9 | QGISPLUGINDIR=$${QGISLIBDIR}/qgis 10 | DEFINES += QGISPLUGINDIR=$${QGISPLUGINDIR} 11 | LIBS = -L$${QGISLIBDIR} -lqgis_core -lproj -lqgis_gui 12 | } 13 | macx{ 14 | QGISDIR=/Users/timsutton/apps/qgis.app/Contents/MacOS/ 15 | QGISLIBDIR=$${QGISDIR}/lib 16 | QGISSRCDIR=/Users/timsutton/dev/cpp/qgis/src/ 17 | QGISPLUGINDIR=$${QGISLIBDIR}/qgis 18 | DEFINES += QGISPLUGINDIR=$${QGISPLUGINDIR} 19 | LIBS = -L$${QGISLIBDIR} -lqgis_core -lqgis_gui 20 | system(mkdir -p qgis_example3.app/Contents/MacOS/share/qgis/resources/) 21 | system(cp $${QGISDIR}/share/qgis/resources/srs.db qgis_example3.app/Contents/MacOS/share/qgis/resources/) 22 | } 23 | 24 | INCLUDEPATH = $${QGISDIR}/include/qgis \ 25 | $${QGISSRCDIR}/core \ 26 | $${QGISSRCDIR}/gui \ 27 | $${QGISSRCDIR}/plugins \ 28 | $${QGISSRCDIR}/providers \ 29 | $${QGISSRCDIR}/raster \ 30 | $${QGISSRCDIR}/ui 31 | 32 | CONFIG += qt gui exceptions stl warn_on debug thread 33 | 34 | #RESOURCES += resources.qrc 35 | 36 | FORMS += mainwindowbase.ui 37 | 38 | HEADERS = mainwindow.h 39 | 40 | SOURCES = main.cpp \ 41 | mainwindow.cpp 42 | 43 | -------------------------------------------------------------------------------- /6_accessing_vector_attributes/data/test.dbf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/6_accessing_vector_attributes/data/test.dbf -------------------------------------------------------------------------------- /6_accessing_vector_attributes/data/test.prj: -------------------------------------------------------------------------------- 1 | GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]] -------------------------------------------------------------------------------- /6_accessing_vector_attributes/data/test.shp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/6_accessing_vector_attributes/data/test.shp -------------------------------------------------------------------------------- /6_accessing_vector_attributes/data/test.shx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/6_accessing_vector_attributes/data/test.shx -------------------------------------------------------------------------------- /6_accessing_vector_attributes/main.cpp: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * Copyright (C) 2006 by Tim Sutton * 3 | * tim@linfiniti.com * 4 | * * 5 | * This program is free software; you can redistribute it and/or modify * 6 | * it under the terms of the GNU General Public License as published by * 7 | * the Free Software Foundation; either version 2 of the License, or * 8 | * (at your option) any later version. * 9 | * * 10 | * This program is distributed in the hope that it will be useful, * 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 13 | * GNU General Public License for more details. * 14 | * * 15 | * You should have received a copy of the GNU General Public License * 16 | * along with this program; if not, write to the * 17 | * Free Software Foundation, Inc., * 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 19 | ***************************************************************************/ 20 | // 21 | // Qt Includes 22 | // 23 | #include 24 | #include 25 | // 26 | // QGIS Includes 27 | // 28 | #include 29 | 30 | int main(int argc, char ** argv) 31 | { 32 | // Start the Application 33 | QgsApplication app(argc, argv, TRUE); 34 | MainWindow * mypMainWindow = new MainWindow(); 35 | mypMainWindow->show(); 36 | // Start the Application Event Loop 37 | return app.exec(); 38 | } 39 | -------------------------------------------------------------------------------- /6_accessing_vector_attributes/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * Copyright (C) 2006 by Tim Sutton * 3 | * tim@linfiniti.com * 4 | * * 5 | * This program is free software; you can redistribute it and/or modify * 6 | * it under the terms of the GNU General Public License as published by * 7 | * the Free Software Foundation; either version 2 of the License, or * 8 | * (at your option) any later version. * 9 | * * 10 | * This program is distributed in the hope that it will be useful, * 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 13 | * GNU General Public License for more details. * 14 | * * 15 | * You should have received a copy of the GNU General Public License * 16 | * along with this program; if not, write to the * 17 | * Free Software Foundation, Inc., * 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 19 | ***************************************************************************/ 20 | #include "mainwindow.h" 21 | // 22 | // QGIS Includes 23 | // 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | 33 | MainWindow::MainWindow(QWidget* parent, Qt::WFlags fl) 34 | : QMainWindow(parent,fl) 35 | { 36 | //required by Qt4 to initialise the ui 37 | setupUi(this); 38 | 39 | // Instantiate Provider Registry 40 | #if defined(Q_WS_MAC) 41 | QString myPluginsDir = "/Users/timsutton/apps/qgis.app/Contents/MacOS/lib/qgis"; 42 | #else 43 | QString myPluginsDir = "/home/timlinux/apps/lib/qgis"; 44 | #endif 45 | QgsProviderRegistry::instance(myPluginsDir); 46 | 47 | addLayer(); 48 | } 49 | 50 | MainWindow::~MainWindow() 51 | { 52 | } 53 | 54 | void MainWindow::addLayer() 55 | { 56 | QString myLayerPath = "data"; 57 | QString myLayerBaseName = "test"; 58 | QString myProviderName = "ogr"; 59 | 60 | QgsVectorLayer * mypLayer = new QgsVectorLayer(myLayerPath, myLayerBaseName, myProviderName); 61 | 62 | if (mypLayer->isValid()) 63 | { 64 | qDebug("Layer is valid"); 65 | } 66 | else 67 | { 68 | qDebug("Layer is NOT valid"); 69 | return; 70 | } 71 | 72 | //get the field list associated with the layer 73 | std::vector myFields = mypLayer->fields(); 74 | //we will hold the list of attributes in this string 75 | QString myString; 76 | //print out the field names first 77 | for (unsigned int i = 0; i < myFields.size(); i++ ) 78 | { 79 | //a little logic so we can produce output like : 80 | // "foo","bar","etc" 81 | if (i==0) 82 | { 83 | // here is where we actually get the field value 84 | myString = "\"" + myFields[i].name().trimmed() + "\""; 85 | } 86 | else 87 | { 88 | myString += ",\"" + myFields[i].name().trimmed() + "\""; 89 | } 90 | } 91 | textBrowser->append("Field List: " + myString); 92 | //get the provider associated with the layer 93 | //the provider handles data io and is a plugin in qgis. 94 | QgsVectorDataProvider* mypProvider=mypLayer->getDataProvider(); 95 | //check the provider is valid 96 | if(mypProvider) 97 | { 98 | //create a holder for retrieving features from the provider 99 | QgsFeature *mypFeature; 100 | //get the provider ready to iterate through it 101 | mypProvider->reset(); 102 | //now iterate through each feature 103 | while ((mypFeature = mypProvider->getNextFeature(true))) 104 | { 105 | //get the attributes of this feature 106 | const std::vector < QgsFeatureAttribute >& myAttributes = mypFeature->attributeMap(); 107 | //now loop through the attributes 108 | for (int i = 0; i < myAttributes.size(); i++) 109 | { 110 | //a little logic so we can produce output like : 111 | // "foo","bar","etc" 112 | if (i==0) 113 | { 114 | // here is where we actually get the field value 115 | myString = "\"" + myAttributes[i].fieldValue().trimmed() + "\""; 116 | } 117 | else 118 | { 119 | myString += ",\"" + myAttributes[i].fieldValue().trimmed() + "\""; 120 | } 121 | } 122 | textBrowser->append("Field Values: " + myString); 123 | //clean up before moving on to the next feature 124 | delete mypFeature; 125 | } 126 | } 127 | } 128 | 129 | -------------------------------------------------------------------------------- /6_accessing_vector_attributes/mainwindow.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * Copyright (C) 2006 by Tim Sutton * 3 | * tim@linfiniti.com * 4 | * * 5 | * This program is free software; you can redistribute it and/or modify * 6 | * it under the terms of the GNU General Public License as published by * 7 | * the Free Software Foundation; either version 2 of the License, or * 8 | * (at your option) any later version. * 9 | * * 10 | * This program is distributed in the hope that it will be useful, * 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 13 | * GNU General Public License for more details. * 14 | * * 15 | * You should have received a copy of the GNU General Public License * 16 | * along with this program; if not, write to the * 17 | * Free Software Foundation, Inc., * 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 19 | ***************************************************************************/ 20 | #ifndef OMGMAINWINDOW_H 21 | #define OMGMAINWINDOW_H 22 | 23 | //QT Includes 24 | #include 25 | 26 | //Local Includes 27 | #include 28 | 29 | /** 30 | @author Tim Sutton 31 | */ 32 | class MainWindow : public QMainWindow, private Ui::MainWindowBase 33 | { 34 | Q_OBJECT; 35 | public: 36 | MainWindow(QWidget* parent = 0, Qt::WFlags fl = 0 ); 37 | ~MainWindow(); 38 | public slots: 39 | void addLayer(); 40 | 41 | private: 42 | }; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /6_accessing_vector_attributes/mainwindowbase.ui: -------------------------------------------------------------------------------- 1 | 2 | MainWindowBase 3 | 4 | 5 | 6 | 0 7 | 0 8 | 579 9 | 330 10 | 11 | 12 | 13 | MainWindow 14 | 15 | 16 | 17 | 18 | 9 19 | 20 | 21 | 6 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 0 32 | 0 33 | 579 34 | 22 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /6_accessing_vector_attributes/resources.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /6_accessing_vector_attributes/tutorial6.html: -------------------------------------------------------------------------------- 1 | In my last tutorial I showed you how to create temporary objects on the map canvas. Today I will giving you a basic introduction to using QgsFeatureAttribute - which allows your to retrieve the attributes for a feature in a vector layer. The attributes are descriptive data related to the geometry of the feature. 2 | 3 | First lets take a look at what we will achieve in this tutorial. 4 | 5 | 6 | 7 | 8 | 9 | 10 | I suggest looking at the other tutorials before going through this one, though its not essential. 11 | 12 |

As with my previous tutorials, the entire project can be checked out from the QGIS Subversion repository using the following command:

13 | 14 |
svn co https://svn.qgis.org/repos/qgis/trunk/code_examples/6_accessing_vector_attributes
 15 | 
16 | 17 |

In the working directory for the tutorial code you will find a number of files including c++ sources and a simple data file under data. There is also the .ui file for the main window.

18 | 19 |

Note: You will need to edit the .pro file in the above svn directory to match your system. Also at some points in the code there are hard coded paths - you should adjust these as appropriate. 20 | 21 | 22 |

QgsFeatureAttribute

23 | 24 | You can browse the code for QgsFeatureAttribute and related classes using the QGIS source browser. Look in the 'core' and 'gui' subdirectories. The classes we are particularly interested in are QgsFeature, QgsFeatureAttribute and QgsProvider. Also for more implementation examples, take a look at 25 | qgsattributetable.cpp. Now on with our demo application.... 26 | 27 | 28 | In mainwindow.h you will notice the includes needed to work with feature attributes: 29 | 30 |
 31 |     #include <qgsfeature.h>
 32 |     #include <qgsfeatureattribute.h>
 33 | 
34 | 35 | There are two main parts to this tutorial, both implmented in addLayer(). The first part lists the field names for the layer and puts it into our QTextBrowser on the form. The second part prints the values in each field for each feature in our sample data layer. Lets start by looking at the code to print the field names. 36 | 37 |
 38 |   //get the field list associated with the layer
 39 |   std::vector myFields = mypLayer->fields();
 40 |   //we will hold the list of attributes in this string
 41 |   QString myString;
 42 |   //print out the field names first
 43 |   for (unsigned int i = 0; i < myFields.size(); i++ )
 44 |   {
 45 |     //a little logic so we can produce output like : 
 46 |     // "foo","bar","etc"
 47 |     if (i==0)
 48 |     {
 49 |       //                 here is where we actually get the field value
 50 |       myString = "\"" +  myFields[i].name().trimmed() + "\"";
 51 |     }
 52 |     else
 53 |     {
 54 |       myString += ",\"" +  myFields[i].name().trimmed() + "\"";
 55 |     }
 56 |   }
 57 |   textBrowser->append("Field List: " + myString);
 58 | 
59 | 60 | If this looks a little familiar, don't be surprised - I used a similar approach in my labelling tutorial. The procedure is straight forward - we ask the layer for its fields, which are returned as a vector. Then we loop through the vector adding the name() value of each field to our string. The trimmed() function just strips off any white space on a QString. 61 | 62 | Next we want to loop through the features to print out their attributes: 63 | 64 |
 65 |   //get the provider associated with the layer
 66 |   //the provider handles data io and is a plugin in qgis.
 67 |   QgsVectorDataProvider* mypProvider=mypLayer->getDataProvider();
 68 |   //check the provider is valid
 69 |   if(mypProvider)
 70 |   {
 71 |     //create a holder for retrieving features from the provider
 72 |     QgsFeature *mypFeature;
 73 |     //get the provider ready to iterate through it
 74 |     mypProvider->reset();
 75 |     //now iterate through each feature
 76 |     while ((mypFeature = mypProvider->getNextFeature(true)))
 77 |     {
 78 |       //get the attributes of this feature
 79 |       const std::vector < QgsFeatureAttribute >& myAttributes = mypFeature->attributeMap();
 80 |       //now loop through the attributes
 81 |       for (int i = 0; i < myAttributes.size(); i++)
 82 |       {
 83 |         //a little logic so we can produce output like : 
 84 |         // "foo","bar","etc"
 85 |         if (i==0)
 86 |         {
 87 |           //                 here is where we actually get the field value
 88 |           myString = "\"" +  myAttributes[i].fieldValue().trimmed() + "\"";
 89 |         }
 90 |         else
 91 |         {
 92 |           myString += ",\"" +  myAttributes[i].fieldValue().trimmed() + "\"";
 93 |         }
 94 |       }
 95 |       textBrowser->append("Field Values: " + myString); 
 96 |       //clean up before moving on to the next feature
 97 |       delete mypFeature;
 98 |     }
 99 |   }
100 | 
101 | 
102 | 103 | I've added in lots of comments in the code, so it should be pretty easy to understand what is going on. The basic steps are to get the provider for the layer, then ask it for features using getNextFeature() until no more features are returned. You can think of the features like a forward only cursor. You cant randomly jump from one record to the next, only iterate through them sequentially. For each feature we retrieve the list of attributes belonging to that feature and add them to a string to be appended to our QTextBrowser. Once we are done with the feature, we delete it to clean up. 104 | 105 | QgsFeature is a cool thing - it lets you know all about the data related to a feature. Whats especially nice in QGIS is that the vector providers are abstracted, so you can use exactly the same procedure to read data from any supported OGR datasource, PostGIS, GPX etc. As more vector providers are added in the future you will automatically gain the ability to read attributes from their features thanks to the standard provider interface. Thats the end of tutorial6! 106 | 107 | 108 |

Mac Specific Notes

109 | 110 | After building the application bundle (qmake; make) you can copy the spatial reference system database into the bundle to avoid 'cant find resource' type errors: 111 | 112 |
113 | mkdir -p qgis_example6.app/Contents/MacOS/share/qgis/resources/ 
114 | cp -r /Applications/qgis.app/Contents/MacOS/share/qgis/resources/* \ 
115 |          qgis_example6.app/Contents/MacOS/share/qgis/resources/
116 | 
117 | -------------------------------------------------------------------------------- /7_writing_custom_maptools/7_writing_custom_maptools.pro: -------------------------------------------------------------------------------- 1 | TEMPLATE = app 2 | TARGET = qgis_example7 3 | QT = qt3support sql opengl network svg gui core xml 4 | LANGUAGE= C++ 5 | linux-g++{ 6 | QGISDIR=/home/timlinux/apps 7 | #QGISDIR=[path to installed qgis] 8 | QGISLIBDIR=$${QGISDIR}/lib 9 | QGISSRCDIR=/home/timlinux/dev/cpp/qgis/src 10 | #QGISSRCDIR=[path to qgis src directory] 11 | QGISPLUGINDIR=$${QGISLIBDIR}/qgis 12 | DEFINES += QGISPLUGINDIR=$${QGISPLUGINDIR} 13 | LIBS = -L$${QGISLIBDIR} -lqgis_core -lproj -lqgis_gui -lqgis_raster 14 | } 15 | macx{ 16 | QGISDIR=/Users/timsutton/apps/qgis.app/Contents/MacOS/ 17 | QGISLIBDIR=$${QGISDIR}/lib 18 | QGISSRCDIR=/Users/timsutton/dev/cpp/qgis/src/ 19 | QGISPLUGINDIR=$${QGISLIBDIR}/qgis 20 | DEFINES += QGISPLUGINDIR=$${QGISPLUGINDIR} 21 | LIBS = -L$${QGISLIBDIR} -lqgis_core -lqgis_gui -lqgis_raster 22 | system(mkdir -p qgis_example3.app/Contents/MacOS/share/qgis/resources/) 23 | system(cp $${QGISDIR}/share/qgis/resources/srs.db qgis_example3.app/Contents/MacOS/share/qgis/resources/) 24 | } 25 | 26 | INCLUDEPATH = $${QGISDIR}/include/qgis \ 27 | $${QGISSRCDIR}/core \ 28 | $${QGISSRCDIR}/gui \ 29 | $${QGISSRCDIR}/plugins \ 30 | $${QGISSRCDIR}/providers \ 31 | $${QGISSRCDIR}/raster \ 32 | $${QGISSRCDIR}/ui 33 | 34 | CONFIG += qt gui exceptions stl warn_on debug thread 35 | 36 | RESOURCES += resources.qrc 37 | 38 | FORMS += mainwindowbase.ui 39 | 40 | HEADERS = mainwindow.h \ 41 | maptooldriller.h 42 | 43 | SOURCES = main.cpp \ 44 | maptooldriller.cpp \ 45 | mainwindow.cpp 46 | 47 | -------------------------------------------------------------------------------- /7_writing_custom_maptools/data/Abarema_jupunba_projection.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/7_writing_custom_maptools/data/Abarema_jupunba_projection.tif -------------------------------------------------------------------------------- /7_writing_custom_maptools/mActionAddLayer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/7_writing_custom_maptools/mActionAddLayer.png -------------------------------------------------------------------------------- /7_writing_custom_maptools/mActionPan.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/7_writing_custom_maptools/mActionPan.png -------------------------------------------------------------------------------- /7_writing_custom_maptools/mActionZoomIn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/7_writing_custom_maptools/mActionZoomIn.png -------------------------------------------------------------------------------- /7_writing_custom_maptools/mActionZoomOut.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/7_writing_custom_maptools/mActionZoomOut.png -------------------------------------------------------------------------------- /7_writing_custom_maptools/main.cpp: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * Copyright (C) 2006 by Tim Sutton * 3 | * tim@linfiniti.com * 4 | * * 5 | * This program is free software; you can redistribute it and/or modify * 6 | * it under the terms of the GNU General Public License as published by * 7 | * the Free Software Foundation; either version 2 of the License, or * 8 | * (at your option) any later version. * 9 | * * 10 | * This program is distributed in the hope that it will be useful, * 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 13 | * GNU General Public License for more details. * 14 | * * 15 | * You should have received a copy of the GNU General Public License * 16 | * along with this program; if not, write to the * 17 | * Free Software Foundation, Inc., * 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 19 | ***************************************************************************/ 20 | // 21 | // Qt Includes 22 | // 23 | #include 24 | #include 25 | // 26 | // QGIS Includes 27 | // 28 | #include 29 | 30 | int main(int argc, char ** argv) 31 | { 32 | // Start the Application 33 | QgsApplication app(argc, argv, TRUE); 34 | MainWindow * mypMainWindow = new MainWindow(); 35 | mypMainWindow->show(); 36 | // Start the Application Event Loop 37 | return app.exec(); 38 | } 39 | -------------------------------------------------------------------------------- /7_writing_custom_maptools/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * Copyright (C) 2006 by Tim Sutton * 3 | * tim@linfiniti.com * 4 | * * 5 | * This program is free software; you can redistribute it and/or modify * 6 | * it under the terms of the GNU General Public License as published by * 7 | * the Free Software Foundation; either version 2 of the License, or * 8 | * (at your option) any later version. * 9 | * * 10 | * This program is distributed in the hope that it will be useful, * 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 13 | * GNU General Public License for more details. * 14 | * * 15 | * You should have received a copy of the GNU General Public License * 16 | * along with this program; if not, write to the * 17 | * Free Software Foundation, Inc., * 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 19 | ***************************************************************************/ 20 | #include "mainwindow.h" 21 | #include "maptooldriller.h" 22 | // 23 | // QGIS Includes 24 | // 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | // 32 | // QGIS Map tools 33 | // 34 | #include "qgsmaptoolpan.h" 35 | #include "qgsmaptoolzoom.h" 36 | // 37 | // Std Includes 38 | // 39 | #include 40 | 41 | MainWindow::MainWindow(QWidget* parent, Qt::WFlags fl) 42 | : QMainWindow(parent,fl) 43 | { 44 | //required by Qt4 to initialise the ui 45 | setupUi(this); 46 | 47 | // Instantiate Provider Registry 48 | #if defined(Q_WS_MAC) 49 | QString myPluginsDir = "/Users/timsutton/apps/qgis.app/Contents/MacOS/lib/qgis"; 50 | #else 51 | QString myPluginsDir = "/home/timlinux/apps/lib/qgis"; 52 | #endif 53 | QgsProviderRegistry::instance(myPluginsDir); 54 | 55 | 56 | // Create the Map Canvas 57 | mpMapCanvas= new QgsMapCanvas(0, 0); 58 | qDebug(mpMapCanvas->extent().stringRep(2)); 59 | mpMapCanvas->enableAntiAliasing(true); 60 | mpMapCanvas->useQImageToRender(false); 61 | mpMapCanvas->setCanvasColor(QColor(255, 255, 255)); 62 | mpMapCanvas->freeze(false); 63 | mpMapCanvas->setVisible(true); 64 | mpMapCanvas->refresh(); 65 | mpMapCanvas->show(); 66 | 67 | // Lay our widgets out in the main window 68 | mpLayout = new QVBoxLayout(frameMap); 69 | mpLayout->addWidget(mpMapCanvas); 70 | 71 | //create the action behaviours 72 | connect(mpActionPan, SIGNAL(triggered()), this, SLOT(panMode())); 73 | connect(mpActionZoomIn, SIGNAL(triggered()), this, SLOT(zoomInMode())); 74 | connect(mpActionZoomOut, SIGNAL(triggered()), this, SLOT(zoomOutMode())); 75 | connect(mpActionAddLayer, SIGNAL(triggered()), this, SLOT(addLayer())); 76 | 77 | //create a little toolbar 78 | mpMapToolBar = addToolBar(tr("File")); 79 | mpMapToolBar->addAction(mpActionAddLayer); 80 | mpMapToolBar->addAction(mpActionZoomIn); 81 | mpMapToolBar->addAction(mpActionZoomOut); 82 | mpMapToolBar->addAction(mpActionPan); 83 | mpMapToolBar->addAction(mpDrillTool); 84 | 85 | //create the maptools 86 | mpPanTool = new QgsMapToolPan(mpMapCanvas); 87 | mpPanTool->setAction(mpActionPan); 88 | mpZoomInTool = new QgsMapToolZoom(mpMapCanvas, FALSE); // false = in 89 | mpZoomInTool->setAction(mpActionZoomIn); 90 | mpZoomOutTool = new QgsMapToolZoom(mpMapCanvas, TRUE ); //true = out 91 | mpZoomOutTool->setAction(mpActionZoomOut); 92 | mpDrillTool = new MapToolDriller(mpMapCanvas); 93 | mpDrillTool->setAction(mpActionDrill); 94 | } 95 | 96 | MainWindow::~MainWindow() 97 | { 98 | delete mpZoomInTool; 99 | delete mpZoomOutTool; 100 | delete mpPanTool; 101 | delete mpDrillTool; 102 | delete mpMapToolBar; 103 | delete mpMapCanvas; 104 | delete mpLayout; 105 | } 106 | 107 | void MainWindow::panMode() 108 | { 109 | mpMapCanvas->setMapTool(mpPanTool); 110 | 111 | } 112 | void MainWindow::zoomInMode() 113 | { 114 | mpMapCanvas->setMapTool(mpZoomInTool); 115 | } 116 | void MainWindow::zoomOutMode() 117 | { 118 | mpMapCanvas->setMapTool(mpZoomOutTool); 119 | } 120 | void MainWindow::addLayer() 121 | { 122 | QFileInfo myRasterFileInfo("data/Abarema_jupunba_projection.tif"); 123 | QgsRasterLayer * mypLayer = new QgsRasterLayer(myRasterFileInfo.filePath(), 124 | myRasterFileInfo.completeBaseName()); 125 | if (mypLayer->isValid()) 126 | { 127 | qDebug("Layer is valid"); 128 | } 129 | else 130 | { 131 | qDebug("Layer is NOT valid"); 132 | return; 133 | } 134 | mypLayer->setColorRampingType(QgsRasterLayer::BLUE_GREEN_RED); 135 | mypLayer->setDrawingStyle(QgsRasterLayer::SINGLE_BAND_PSEUDO_COLOR); 136 | std::deque myLayerSet; 137 | 138 | // Add the Vector Layer to the Layer Registry 139 | QgsMapLayerRegistry::instance()->addMapLayer(mypLayer, TRUE); 140 | 141 | // Add the Layer to the Layer Set 142 | myLayerSet.push_back(mypLayer->getLayerID()); 143 | mypLayer->setVisible(TRUE); 144 | // set teh canvas to the extent of our layer 145 | mpMapCanvas->setExtent(mypLayer->extent()); 146 | // Set the Map Canvas Layer Set 147 | mpMapCanvas->setLayerSet(myLayerSet); 148 | } 149 | 150 | -------------------------------------------------------------------------------- /7_writing_custom_maptools/mainwindow.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * Copyright (C) 2006 by Tim Sutton * 3 | * tim@linfiniti.com * 4 | * * 5 | * This program is free software; you can redistribute it and/or modify * 6 | * it under the terms of the GNU General Public License as published by * 7 | * the Free Software Foundation; either version 2 of the License, or * 8 | * (at your option) any later version. * 9 | * * 10 | * This program is distributed in the hope that it will be useful, * 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 13 | * GNU General Public License for more details. * 14 | * * 15 | * You should have received a copy of the GNU General Public License * 16 | * along with this program; if not, write to the * 17 | * Free Software Foundation, Inc., * 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 19 | ***************************************************************************/ 20 | #ifndef OMGMAINWINDOW_H 21 | #define OMGMAINWINDOW_H 22 | 23 | #include "maptooldriller.h" 24 | //QGis Includes 25 | #include 26 | #include 27 | 28 | //QT Includes 29 | #include 30 | 31 | //Local Includes 32 | #include 33 | 34 | /** 35 | @author Tim Sutton 36 | */ 37 | class MainWindow : public QMainWindow, private Ui::MainWindowBase 38 | { 39 | Q_OBJECT; 40 | public: 41 | MainWindow(QWidget* parent = 0, Qt::WFlags fl = 0 ); 42 | ~MainWindow(); 43 | public slots: 44 | void zoomInMode(); 45 | void zoomOutMode(); 46 | void panMode(); 47 | void addLayer(); 48 | 49 | private: 50 | QgsMapCanvas * mpMapCanvas; 51 | QVBoxLayout * mpLayout; 52 | QToolBar * mpMapToolBar; 53 | QgsMapTool * mpPanTool; 54 | QgsMapTool * mpZoomInTool; 55 | QgsMapTool * mpZoomOutTool; 56 | MapToolDriller * mpDrillTool; 57 | 58 | }; 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /7_writing_custom_maptools/mainwindowbase.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | MainWindowBase 6 | 7 | 8 | 9 | 0 10 | 0 11 | 579 12 | 330 13 | 14 | 15 | 16 | MainWindow 17 | 18 | 19 | 20 | 21 | 9 22 | 23 | 24 | 6 25 | 26 | 27 | 28 | 29 | QFrame::StyledPanel 30 | 31 | 32 | QFrame::Raised 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 0 42 | 0 43 | 579 44 | 22 45 | 46 | 47 | 48 | 49 | Map 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | :/mActionZoomIn.png 62 | 63 | 64 | Zoom In 65 | 66 | 67 | 68 | 69 | :/mActionZoomOut.png 70 | 71 | 72 | Zoom Out 73 | 74 | 75 | 76 | 77 | :/mActionPan.png 78 | 79 | 80 | Pan 81 | 82 | 83 | 84 | 85 | :/mActionAddLayer.png 86 | 87 | 88 | Add Layer 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | -------------------------------------------------------------------------------- /7_writing_custom_maptools/maptooldriller.cpp: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | qgsmaptooldriller.h - This tool will drill through a bunch of rasters 3 | and produce a table of values at a given point 4 | in each layer 5 | --------------------- 6 | begin : November 2006 7 | copyright : (C) 2006 by Tim Sutton 8 | email : tim at linfiniti.com 9 | *************************************************************************** 10 | * * 11 | * This program is free software; you can redistribute it and/or modify * 12 | * it under the terms of the GNU General Public License as published by * 13 | * the Free Software Foundation; either version 2 of the License, or * 14 | * (at your option) any later version. * 15 | * * 16 | ***************************************************************************/ 17 | /* $Id$ */ 18 | 19 | #include "maptooldriller.h" 20 | #include "qgsmapcanvas.h" 21 | #include "qgsrasterlayer.h" 22 | #include "qgsmaptopixel.h" 23 | #include "qgscursors.h" 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | MapToolDriller::MapToolDriller(QgsMapCanvas* canvas) 31 | : QObject(), QgsMapTool(canvas) 32 | { 33 | // set cursor 34 | QPixmap myIdentifyQPixmap = QPixmap((const char **) identify_cursor); 35 | mCursor = QCursor(myIdentifyQPixmap, 1, 1); 36 | } 37 | 38 | MapToolDriller::~MapToolDriller() 39 | { 40 | } 41 | 42 | void MapToolDriller::canvasMoveEvent(QMouseEvent * e) 43 | { 44 | 45 | } 46 | 47 | void MapToolDriller::canvasPressEvent(QMouseEvent * e) 48 | { 49 | } 50 | 51 | void MapToolDriller::canvasReleaseEvent(QMouseEvent * e) 52 | { 53 | 54 | QgsPoint myPoint = mCanvas->getCoordinateTransform()->toMapCoordinates(e->x(), e->y()); 55 | QHash myHash; 56 | myHash = drill(myPoint); 57 | emit drilled(myHash); 58 | } 59 | 60 | QHash MapToolDriller::drill(const QgsPoint &thePoint) 61 | { 62 | QHash myHash; 63 | //ok so here is where the real work is done 64 | //we iterate through the layers in the map canvas 65 | //and check if each layer is a raster 66 | //if it is we add the looked up value for that raster to our 67 | //hash table 68 | 69 | QgsMapLayer* layer = mCanvas->currentLayer(); 70 | 71 | // call identify method for selected layer 72 | 73 | if (layer) 74 | { 75 | // convert screen coordinates to map coordinates 76 | 77 | if (layer->type() == QgsMapLayer::VECTOR) 78 | { 79 | //do nothing 80 | } 81 | else if (layer->type() == QgsMapLayer::RASTER) 82 | { 83 | if (dynamic_cast(layer)->providerKey() == "wms") 84 | { 85 | //do nothing 86 | } 87 | else 88 | { 89 | std::map myAttributes; 90 | dynamic_cast(layer)->identify(thePoint, myAttributes); 91 | } 92 | } 93 | else 94 | { 95 | #ifdef QGISDEBUG 96 | std::cout << "MapToolDriller::canvasReleaseEvent: unknown layer type!" << std::endl; 97 | #endif 98 | } 99 | } 100 | return myHash; 101 | } 102 | 103 | 104 | void MapToolDriller::deactivate() 105 | { 106 | } 107 | -------------------------------------------------------------------------------- /7_writing_custom_maptools/maptooldriller.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | qgsmaptooldriller.h - This tool will drill through a bunch of rasters 3 | and produce a table of values at a given point 4 | in each layer 5 | --------------------- 6 | begin : November 2006 7 | copyright : (C) 2006 by Tim Sutton 8 | email : tim at linfiniti.com 9 | *************************************************************************** 10 | * * 11 | * This program is free software; you can redistribute it and/or modify * 12 | * it under the terms of the GNU General Public License as published by * 13 | * the Free Software Foundation; either version 2 of the License, or * 14 | * (at your option) any later version. * 15 | * * 16 | ***************************************************************************/ 17 | /* $Id$ */ 18 | 19 | #ifndef MAPTOOLDRILLER_H 20 | #define MAPTOOLDRILLER_H 21 | 22 | #include "qgsmaptool.h" 23 | #include "qgspoint.h" 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | class QgsMapLayer; 30 | class QgsRasterLayer; 31 | class QgsVectorLayer; 32 | 33 | /** 34 | \brief Map tool for drilling down through several raster layers and 35 | returning a hash of the results. 36 | */ 37 | class MapToolDriller : public QObject, public QgsMapTool 38 | { 39 | Q_OBJECT; 40 | 41 | public: 42 | MapToolDriller(QgsMapCanvas* canvas); 43 | 44 | ~MapToolDriller(); 45 | 46 | //! Overridden mouse move event 47 | virtual void canvasMoveEvent(QMouseEvent * e); 48 | 49 | //! Overridden mouse press event 50 | virtual void canvasPressEvent(QMouseEvent * e); 51 | 52 | //! Overridden mouse release event 53 | virtual void canvasReleaseEvent(QMouseEvent * e); 54 | 55 | //! called when map tool is being deactivated 56 | virtual void deactivate(); 57 | 58 | typedef QHash DrillResult; 59 | signals: 60 | void drilled(DrillResult); 61 | 62 | 63 | 64 | private: 65 | QHash drill(const QgsPoint& myPoint); 66 | private slots: 67 | 68 | 69 | }; 70 | 71 | #endif 72 | -------------------------------------------------------------------------------- /7_writing_custom_maptools/resources.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | mActionAddLayer.png 4 | mActionPan.png 5 | mActionZoomIn.png 6 | mActionZoomOut.png 7 | 8 | 9 | -------------------------------------------------------------------------------- /generate_docs.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # 4 | # Simple bash script to generate tutorial documentation 5 | # 6 | 7 | txt2tags -t html -o index.html index.t2t 8 | txt2tags -t html -o tutorial1.html 1_hello_world_qgis_style/tutorial1.t2t 9 | txt2tags -t html -o tutorial2.html 2_basic_main_window/tutorial2.t2t 10 | txt2tags -t html -o tutorial3.html 3_basic_labelling/tutorial3.t2t 11 | txt2tags -t txt -o README index.t2t 12 | 13 | -------------------------------------------------------------------------------- /images/tim50x50.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/images/tim50x50.png -------------------------------------------------------------------------------- /images/tutorial1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/images/tutorial1.jpg -------------------------------------------------------------------------------- /images/tutorial2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/images/tutorial2.jpg -------------------------------------------------------------------------------- /images/tutorial3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/images/tutorial3.jpg -------------------------------------------------------------------------------- /index.t2t: -------------------------------------------------------------------------------- 1 | QGIS Developer Tutorials 2 | 3 | Tim Sutton, 2008 4 | 5 | %! target : html 6 | %! style : style.css 7 | %! Options : --toc --toc-level 3 --enum-title --css-sugar --css-inside 8 | %! preproc : TUT_URL https://qgis.org 9 | %! PostProc(html): '(?i)(```)' '
\1' 10 | %! PostProc(html): '(?i)(```)' '\1
' 11 | %! encoding: iso-8859-1 12 | % These are comments and will not be generated in any output 13 | % ------------------- 14 | 15 | %This document is in text2tags format. You can generate html, plain text and 16 | %moinmoin formatted documentation by running txt2tags on this document. See the 17 | %txt2tags home page for more details. Please insert manual line breaks in this 18 | %document as it makes diffing for changes much easier. To do this in vim 19 | %automatically, select a section then issue (gq) command. Please dont 20 | %apply vim formatting to the whol document as it screws up some formatting 21 | %rather apply it selectively to paragraphs where needed. 22 | 23 | % To generate the text version of this document: 24 | % txt2tags -t txt -o index index.t2t 25 | % To generate the moinmoin version of this document 26 | % txt2tags -t moin -o index.moin index.t2t 27 | % To generate the html version of this document 28 | % txt2tags -t html -o index.html index.t2t 29 | 30 | % End of comments 31 | % ------------------- 32 | 33 | 34 | %!include: 1_hello_world_qgis_style/tutorial1.t2t 35 | %!include: 2_basic_main_window/tutorial2.t2t 36 | %!include: 3_basic_labelling/tutorial3.t2t 37 | -------------------------------------------------------------------------------- /plugin_writer_workshop/plugin_writer.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/plugin_writer_workshop/plugin_writer.pdf -------------------------------------------------------------------------------- /plugin_writer_workshop/pointconverter_final/plugin_writer.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/plugin_writer_workshop/pointconverter_final/plugin_writer.pdf -------------------------------------------------------------------------------- /plugin_writer_workshop/pointconverter_final/pointconverter.pro: -------------------------------------------------------------------------------- 1 | TEMPLATE = lib 2 | CONFIG = qt 3 | QT += xml qt3support 4 | unix:LIBS += -lqgis_core -lqgis_gui -lqgis_raster -lqgis_legend -lproj 5 | INCLUDEPATH += /usr/include/qgis 6 | SOURCES = qgspointconverterplugin.cpp 7 | HEADERS = qgspointconverterplugin.h 8 | DEST = pointconverterplugin.so 9 | -------------------------------------------------------------------------------- /plugin_writer_workshop/pointconverter_final/qgspointconverterplugin.cpp: -------------------------------------------------------------------------------- 1 | #include "qgspointconverterplugin.h" 2 | #include "qgsvectordataprovider.h" 3 | #include "qgsvectorlayer.h" 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #ifdef WIN32 10 | #define QGISEXTERN extern "C" __declspec( dllexport ) 11 | #else 12 | #define QGISEXTERN extern "C" 13 | #endif 14 | 15 | QgsPointConverterPlugin::QgsPointConverterPlugin(QgisApp* app, QgisIface* iface): mApp(app), mIface(iface), mAction(0) 16 | { 17 | 18 | } 19 | 20 | QgsPointConverterPlugin::~QgsPointConverterPlugin() 21 | { 22 | 23 | } 24 | 25 | void QgsPointConverterPlugin::initGui() 26 | { 27 | mAction = new QAction(tr("&Convert to point"), this); 28 | connect(mAction, SIGNAL(activated()), this, SLOT(convertToPoint())); 29 | // Add the action to toolbar and the plugin menu 30 | mIface->addToolBarIcon(mAction); 31 | mIface->addPluginMenu(tr("&Convert to point"), mAction); 32 | } 33 | 34 | void QgsPointConverterPlugin::unload() 35 | { 36 | mIface->removeToolBarIcon(mAction); 37 | mIface->removePluginMenu(tr("&Convert to point"), mAction); 38 | delete mAction; 39 | } 40 | 41 | void QgsPointConverterPlugin::convertToPoint() 42 | { 43 | qWarning("in convertToPoint()"); 44 | 45 | //get the current layer 46 | QgsMapLayer* theMapLayer = mIface->activeLayer(); 47 | if(!theMapLayer) 48 | { 49 | QMessageBox::information(0, "no active layer", "this plugin needs an active point vector layer to make a conversion to points", QMessageBox::Ok); 50 | return; 51 | } 52 | QgsVectorLayer* theVectorLayer = dynamic_cast(theMapLayer); 53 | if(!theVectorLayer) 54 | { 55 | QMessageBox::information(0, "no vector layer", "this plugin needs an active point vector layer to make a conversion to points", QMessageBox::Ok); 56 | return; 57 | } 58 | 59 | QString fileName = QFileDialog::getSaveFileName(); 60 | 61 | if(!fileName.isNull()) 62 | { 63 | qWarning("the selected file name is: " + fileName); 64 | QFile f(fileName); 65 | if(!f.open(QIODevice::WriteOnly)) 66 | { 67 | QMessageBox::information(0, "error", "Could not open file", QMessageBox::Ok); 68 | } 69 | QTextStream theTextStream(&f); 70 | theTextStream.setRealNumberNotation(QTextStream::FixedNotation); 71 | //iterate over the layer features 72 | theTextStream << "x,y"; 73 | //add the names of the non-geometry attributes 74 | std::vector fieldVector = theVectorLayer->getDataProvider()->fields(); 75 | for(std::vector::const_iterator it = fieldVector.begin(); it != fieldVector.end(); ++it) 76 | { 77 | theTextStream << "," << it->name(); 78 | } 79 | theTextStream << endl; 80 | 81 | QgsFeature* currentFeature = 0; 82 | geos::Geometry* currentGeometry = 0; 83 | geos::CoordinateSequence* currentSequence = 0; 84 | int currentSequenceSize = 0; 85 | 86 | //copy the attributes too 87 | QString featureAttributeString; 88 | std::vector featureAttributeMap; 89 | 90 | theVectorLayer->getDataProvider()->reset(); 91 | while(currentFeature = theVectorLayer->getNextFeature(true, false)) 92 | { 93 | featureAttributeMap = currentFeature->attributeMap(); 94 | featureAttributeString = ""; 95 | for(std::vector::const_iterator it = featureAttributeMap.begin(); it != featureAttributeMap.end(); ++it) 96 | { 97 | featureAttributeString.append(","+it->fieldValue()); 98 | qWarning("appending " + it->fieldValue() + " to attribute map"); 99 | } 100 | currentGeometry = currentFeature->geosGeometry(); 101 | currentSequence = currentGeometry->getCoordinates(); 102 | currentSequenceSize = currentSequence->getSize(); 103 | for(int i = 0; i < currentSequenceSize; ++i) 104 | { 105 | //qWarning("found coordinate: " + QString::number(currentSequence->getAt(i).x) + " // " + QString::number(currentSequence->getAt(i).y)); 106 | theTextStream << currentSequence->getAt(i).x << "," << currentSequence->getAt(i).y << featureAttributeString << endl; 107 | } 108 | delete currentGeometry; 109 | delete currentFeature; 110 | } 111 | } 112 | } 113 | 114 | QGISEXTERN QgisPlugin* classFactory(QgisApp* app, QgisIface* iface) 115 | { 116 | return new QgsPointConverterPlugin(app, iface); 117 | } 118 | 119 | QGISEXTERN QString name() 120 | { 121 | return "point converter plugin"; 122 | } 123 | 124 | QGISEXTERN QString description() 125 | { 126 | return "A plugin that converts vector layers to delimited text point files"; 127 | } 128 | 129 | QGISEXTERN QString version() 130 | { 131 | return "0.00001"; 132 | } 133 | 134 | // Return the type (either UI or MapLayer plugin) 135 | QGISEXTERN int type() 136 | { 137 | return QgisPlugin::UI; 138 | } 139 | 140 | // Delete ourself 141 | QGISEXTERN void unload(QgisPlugin* theQgsPointConverterPluginPointer) 142 | { 143 | delete theQgsPointConverterPluginPointer; 144 | } 145 | 146 | -------------------------------------------------------------------------------- /plugin_writer_workshop/pointconverter_final/qgspointconverterplugin.h: -------------------------------------------------------------------------------- 1 | #ifndef QGSPOINTCONVERTERPLUGIN 2 | #define QGSPOINTCONVERTERPLUGIN 3 | 4 | #include "qgisplugin.h" 5 | #include 6 | 7 | /**A plugin that converts vector layers to delimited text point files. 8 | The vertices of polygon/line type layers are converted to point features*/ 9 | class QgsPointConverterPlugin: public QObject, public QgisPlugin 10 | { 11 | Q_OBJECT 12 | public: 13 | QgsPointConverterPlugin(QgisApp* app, QgisIface* iface); 14 | ~QgsPointConverterPlugin(); 15 | /**Inserts an item into the toolbar*/ 16 | virtual void initGui(); 17 | void unload(); 18 | 19 | private: 20 | QgisApp* mApp; 21 | QgisIface* mIface; 22 | QAction* mAction; 23 | 24 | private slots: 25 | void convertToPoint(); 26 | }; 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /plugin_writer_workshop/pointconverter_step1/libpointconverter.so.1.0.0: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/plugin_writer_workshop/pointconverter_step1/libpointconverter.so.1.0.0 -------------------------------------------------------------------------------- /plugin_writer_workshop/pointconverter_step1/pointconverter.pro: -------------------------------------------------------------------------------- 1 | #base directory of the qgis installation 2 | QGIS_DIR = /home/humarco/src/qgis 3 | 4 | TEMPLATE = lib 5 | CONFIG = qt 6 | QT += xml qt3support 7 | unix:LIBS += -L/$$QGIS_DIR/lib -lqgis_core -lqgis_gui -lqgis_raster -lqgis_legend -lproj 8 | INCLUDEPATH += $$QGIS_DIR/src/ui $$QGIS_DIR/src/plugins $$QGIS_DIR/src/gui $$QGIS_DIR/src/raster $$QGIS_DIR/src/core $$QGIS_DIR 9 | SOURCES = qgspointconverterplugin.cpp 10 | HEADERS = qgspointconverterplugin.h 11 | DEST = pointconverterplugin.so 12 | -------------------------------------------------------------------------------- /plugin_writer_workshop/pointconverter_step1/qgspointconverterplugin.cpp: -------------------------------------------------------------------------------- 1 | #include "qgspointconverterplugin.h" 2 | 3 | #ifdef WIN32 4 | #define QGISEXTERN extern "C" __declspec( dllexport ) 5 | #else 6 | #define QGISEXTERN extern "C" 7 | #endif 8 | 9 | QgsPointConverterPlugin::QgsPointConverterPlugin(QgisApp* app, QgisIface* iface) 10 | { 11 | 12 | } 13 | 14 | QgsPointConverterPlugin::~QgsPointConverterPlugin() 15 | { 16 | 17 | } 18 | 19 | void QgsPointConverterPlugin::initGui() 20 | { 21 | 22 | } 23 | 24 | void QgsPointConverterPlugin::unload() 25 | { 26 | 27 | } 28 | 29 | QGISEXTERN QgisPlugin* classFactory(QgisApp* app, QgisIface* iface) 30 | { 31 | return new QgsPointConverterPlugin(app, iface); 32 | } 33 | 34 | QGISEXTERN QString name() 35 | { 36 | return "point converter plugin"; 37 | } 38 | 39 | QGISEXTERN QString description() 40 | { 41 | return "A plugin that converts vector layers to delimited text point files"; 42 | } 43 | 44 | QGISEXTERN QString version() 45 | { 46 | return "0.00001"; 47 | } 48 | 49 | // Return the type (either UI or MapLayer plugin) 50 | QGISEXTERN int type() 51 | { 52 | return QgisPlugin::UI; 53 | } 54 | 55 | // Delete ourself 56 | QGISEXTERN void unload(QgisPlugin* theQgsPointConverterPluginPointer) 57 | { 58 | delete theQgsPointConverterPluginPointer; 59 | } 60 | 61 | -------------------------------------------------------------------------------- /plugin_writer_workshop/pointconverter_step1/qgspointconverterplugin.h: -------------------------------------------------------------------------------- 1 | #ifndef QGSPOINTCONVERTERPLUGIN_H 2 | #define QGSPOINTCONVERTERPLUGIN_H 3 | 4 | #include "qgisplugin.h" 5 | #include 6 | 7 | /**A plugin that converts vector layers to delimited text point files. 8 | The vertices of polygon/line type layers are converted to point features*/ 9 | class QgsPointConverterPlugin: public QObject, public QgisPlugin 10 | { 11 | public: 12 | QgsPointConverterPlugin(QgisApp* app, QgisIface* iface); 13 | ~QgsPointConverterPlugin(); 14 | void initGui(); 15 | void unload(); 16 | }; 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /plugin_writer_workshop/pointconverter_step2/libpointconverter.so.1.0.0: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/plugin_writer_workshop/pointconverter_step2/libpointconverter.so.1.0.0 -------------------------------------------------------------------------------- /plugin_writer_workshop/pointconverter_step2/moc_qgspointconverterplugin.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | ** Meta object code from reading C++ file 'qgspointconverterplugin.h' 3 | ** 4 | ** Created: Don Sep 7 10:11:34 2006 5 | ** by: The Qt Meta Object Compiler version 59 (Qt 4.1.2) 6 | ** 7 | ** WARNING! All changes made in this file will be lost! 8 | *****************************************************************************/ 9 | 10 | #include "qgspointconverterplugin.h" 11 | #if !defined(Q_MOC_OUTPUT_REVISION) 12 | #error "The header file 'qgspointconverterplugin.h' doesn't include ." 13 | #elif Q_MOC_OUTPUT_REVISION != 59 14 | #error "This file was generated using the moc from 4.1.2. It" 15 | #error "cannot be used with the include files from this version of Qt." 16 | #error "(The moc has changed too much.)" 17 | #endif 18 | 19 | static const uint qt_meta_data_QgsPointConverterPlugin[] = { 20 | 21 | // content: 22 | 1, // revision 23 | 0, // classname 24 | 0, 0, // classinfo 25 | 1, 10, // methods 26 | 0, 0, // properties 27 | 0, 0, // enums/sets 28 | 29 | // slots: signature, parameters, type, tag, flags 30 | 25, 24, 24, 24, 0x08, 31 | 32 | 0 // eod 33 | }; 34 | 35 | static const char qt_meta_stringdata_QgsPointConverterPlugin[] = { 36 | "QgsPointConverterPlugin\0\0convertToPoint()\0" 37 | }; 38 | 39 | const QMetaObject QgsPointConverterPlugin::staticMetaObject = { 40 | { &QObject::staticMetaObject, qt_meta_stringdata_QgsPointConverterPlugin, 41 | qt_meta_data_QgsPointConverterPlugin, 0 } 42 | }; 43 | 44 | const QMetaObject *QgsPointConverterPlugin::metaObject() const 45 | { 46 | return &staticMetaObject; 47 | } 48 | 49 | void *QgsPointConverterPlugin::qt_metacast(const char *_clname) 50 | { 51 | if (!_clname) return 0; 52 | if (!strcmp(_clname, qt_meta_stringdata_QgsPointConverterPlugin)) 53 | return static_cast(const_cast(this)); 54 | if (!strcmp(_clname, "QgisPlugin")) 55 | return static_cast(const_cast(this)); 56 | return QObject::qt_metacast(_clname); 57 | } 58 | 59 | int QgsPointConverterPlugin::qt_metacall(QMetaObject::Call _c, int _id, void **_a) 60 | { 61 | _id = QObject::qt_metacall(_c, _id, _a); 62 | if (_id < 0) 63 | return _id; 64 | if (_c == QMetaObject::InvokeMetaMethod) { 65 | switch (_id) { 66 | case 0: convertToPoint(); break; 67 | } 68 | _id -= 1; 69 | } 70 | return _id; 71 | } 72 | -------------------------------------------------------------------------------- /plugin_writer_workshop/pointconverter_step2/pointconverter.pro: -------------------------------------------------------------------------------- 1 | #base directory of the qgis installation 2 | QGIS_DIR = /home/humarco/src/qgis 3 | 4 | TEMPLATE = lib 5 | CONFIG = qt 6 | QT += xml qt3support 7 | unix:LIBS += -L/$$QGIS_DIR/lib -lqgis_core -lqgis_gui -lqgis_raster -lqgis_legend -lproj 8 | INCLUDEPATH += $$QGIS_DIR/src/ui $$QGIS_DIR/src/plugins $$QGIS_DIR/src/gui $$QGIS_DIR/src/raster $$QGIS_DIR/src/core $$QGIS_DIR 9 | SOURCES = qgspointconverterplugin.cpp 10 | HEADERS = qgspointconverterplugin.h 11 | DEST = pointconverterplugin.so 12 | -------------------------------------------------------------------------------- /plugin_writer_workshop/pointconverter_step2/qgspointconverterplugin.cpp: -------------------------------------------------------------------------------- 1 | #include "qgspointconverterplugin.h" 2 | 3 | #ifdef WIN32 4 | #define QGISEXTERN extern "C" __declspec( dllexport ) 5 | #else 6 | #define QGISEXTERN extern "C" 7 | #endif 8 | 9 | QgsPointConverterPlugin::QgsPointConverterPlugin(QgisApp* app, QgisIface* iface): mIface(iface), mAction(0) 10 | { 11 | 12 | } 13 | 14 | QgsPointConverterPlugin::~QgsPointConverterPlugin() 15 | { 16 | 17 | } 18 | 19 | void QgsPointConverterPlugin::initGui() 20 | { 21 | mAction = new QAction(tr("&Convert to point"), this); 22 | connect(mAction, SIGNAL(activated()), this, SLOT(convertToPoint())); 23 | mIface->addToolBarIcon(mAction); 24 | mIface->addPluginMenu(tr("&Convert to point"), mAction); 25 | } 26 | 27 | void QgsPointConverterPlugin::unload() 28 | { 29 | mIface->removeToolBarIcon(mAction); 30 | mIface->removePluginMenu(tr("&Convert to point"), mAction); 31 | delete mAction; 32 | } 33 | 34 | void QgsPointConverterPlugin::convertToPoint() 35 | { 36 | qWarning("in method convertToPoint"); 37 | } 38 | 39 | QGISEXTERN QgisPlugin* classFactory(QgisApp* app, QgisIface* iface) 40 | { 41 | return new QgsPointConverterPlugin(app, iface); 42 | } 43 | 44 | QGISEXTERN QString name() 45 | { 46 | return "point converter plugin"; 47 | } 48 | 49 | QGISEXTERN QString description() 50 | { 51 | return "A plugin that converts vector layers to delimited text point files"; 52 | } 53 | 54 | QGISEXTERN QString version() 55 | { 56 | return "0.00001"; 57 | } 58 | 59 | // Return the type (either UI or MapLayer plugin) 60 | QGISEXTERN int type() 61 | { 62 | return QgisPlugin::UI; 63 | } 64 | 65 | // Delete ourself 66 | QGISEXTERN void unload(QgisPlugin* theQgsPointConverterPluginPointer) 67 | { 68 | delete theQgsPointConverterPluginPointer; 69 | } 70 | 71 | -------------------------------------------------------------------------------- /plugin_writer_workshop/pointconverter_step2/qgspointconverterplugin.h: -------------------------------------------------------------------------------- 1 | #ifndef QGSPOINTCONVERTERPLUGIN_H 2 | #define QGSPOINTCONVERTERPLUGIN_H 3 | 4 | #include "qgisplugin.h" 5 | #include 6 | 7 | /**A plugin that converts vector layers to delimited text point files. 8 | The vertices of polygon/line type layers are converted to point features*/ 9 | class QgsPointConverterPlugin: public QObject, public QgisPlugin 10 | { 11 | Q_OBJECT 12 | 13 | public: 14 | QgsPointConverterPlugin(QgisApp* app, QgisIface* iface); 15 | ~QgsPointConverterPlugin(); 16 | void initGui(); 17 | void unload(); 18 | 19 | private: 20 | QgisIface* mIface; 21 | QAction* mAction; 22 | 23 | private slots: 24 | void convertToPoint(); 25 | }; 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /plugin_writer_workshop/pointconverter_step3/libpointconverter.so.1.0.0: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qgis/QGIS-Code-Examples/c5bfab26db88445b4b25272992f1fb2b0f714fab/plugin_writer_workshop/pointconverter_step3/libpointconverter.so.1.0.0 -------------------------------------------------------------------------------- /plugin_writer_workshop/pointconverter_step3/moc_qgspointconverterplugin.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | ** Meta object code from reading C++ file 'qgspointconverterplugin.h' 3 | ** 4 | ** Created: Fre Sep 8 07:20:26 2006 5 | ** by: The Qt Meta Object Compiler version 59 (Qt 4.1.2) 6 | ** 7 | ** WARNING! All changes made in this file will be lost! 8 | *****************************************************************************/ 9 | 10 | #include "qgspointconverterplugin.h" 11 | #if !defined(Q_MOC_OUTPUT_REVISION) 12 | #error "The header file 'qgspointconverterplugin.h' doesn't include ." 13 | #elif Q_MOC_OUTPUT_REVISION != 59 14 | #error "This file was generated using the moc from 4.1.2. It" 15 | #error "cannot be used with the include files from this version of Qt." 16 | #error "(The moc has changed too much.)" 17 | #endif 18 | 19 | static const uint qt_meta_data_QgsPointConverterPlugin[] = { 20 | 21 | // content: 22 | 1, // revision 23 | 0, // classname 24 | 0, 0, // classinfo 25 | 1, 10, // methods 26 | 0, 0, // properties 27 | 0, 0, // enums/sets 28 | 29 | // slots: signature, parameters, type, tag, flags 30 | 25, 24, 24, 24, 0x08, 31 | 32 | 0 // eod 33 | }; 34 | 35 | static const char qt_meta_stringdata_QgsPointConverterPlugin[] = { 36 | "QgsPointConverterPlugin\0\0convertToPoint()\0" 37 | }; 38 | 39 | const QMetaObject QgsPointConverterPlugin::staticMetaObject = { 40 | { &QObject::staticMetaObject, qt_meta_stringdata_QgsPointConverterPlugin, 41 | qt_meta_data_QgsPointConverterPlugin, 0 } 42 | }; 43 | 44 | const QMetaObject *QgsPointConverterPlugin::metaObject() const 45 | { 46 | return &staticMetaObject; 47 | } 48 | 49 | void *QgsPointConverterPlugin::qt_metacast(const char *_clname) 50 | { 51 | if (!_clname) return 0; 52 | if (!strcmp(_clname, qt_meta_stringdata_QgsPointConverterPlugin)) 53 | return static_cast(const_cast(this)); 54 | if (!strcmp(_clname, "QgisPlugin")) 55 | return static_cast(const_cast(this)); 56 | return QObject::qt_metacast(_clname); 57 | } 58 | 59 | int QgsPointConverterPlugin::qt_metacall(QMetaObject::Call _c, int _id, void **_a) 60 | { 61 | _id = QObject::qt_metacall(_c, _id, _a); 62 | if (_id < 0) 63 | return _id; 64 | if (_c == QMetaObject::InvokeMetaMethod) { 65 | switch (_id) { 66 | case 0: convertToPoint(); break; 67 | } 68 | _id -= 1; 69 | } 70 | return _id; 71 | } 72 | -------------------------------------------------------------------------------- /plugin_writer_workshop/pointconverter_step3/pointconverter.pro: -------------------------------------------------------------------------------- 1 | #base directory of the qgis installation 2 | QGIS_DIR = /home/humarco/src/qgis 3 | 4 | TEMPLATE = lib 5 | CONFIG = qt 6 | QT += xml qt3support 7 | unix:LIBS += -L/$$QGIS_DIR/lib -lqgis_core -lqgis_gui -lqgis_raster -lqgis_legend -lproj 8 | INCLUDEPATH += $$QGIS_DIR/src/ui $$QGIS_DIR/src/plugins $$QGIS_DIR/src/gui $$QGIS_DIR/src/raster $$QGIS_DIR/src/core $$QGIS_DIR 9 | SOURCES = qgspointconverterplugin.cpp 10 | HEADERS = qgspointconverterplugin.h 11 | DEST = pointconverterplugin.so 12 | -------------------------------------------------------------------------------- /plugin_writer_workshop/pointconverter_step3/qgspointconverterplugin.cpp: -------------------------------------------------------------------------------- 1 | #include "qgspointconverterplugin.h" 2 | #include "qgsvectordataprovider.h" 3 | #include "qgsvectorlayer.h" 4 | #include 5 | #include 6 | #include 7 | 8 | #ifdef WIN32 9 | #define QGISEXTERN extern "C" __declspec( dllexport ) 10 | #else 11 | #define QGISEXTERN extern "C" 12 | #endif 13 | 14 | QgsPointConverterPlugin::QgsPointConverterPlugin(QgisApp* app, QgisIface* iface): mIface(iface), mAction(0) 15 | { 16 | 17 | } 18 | 19 | QgsPointConverterPlugin::~QgsPointConverterPlugin() 20 | { 21 | 22 | } 23 | 24 | void QgsPointConverterPlugin::initGui() 25 | { 26 | mAction = new QAction(tr("&Convert to point"), this); 27 | connect(mAction, SIGNAL(activated()), this, SLOT(convertToPoint())); 28 | mIface->addToolBarIcon(mAction); 29 | mIface->addPluginMenu(tr("&Convert to point"), mAction); 30 | } 31 | 32 | void QgsPointConverterPlugin::unload() 33 | { 34 | mIface->removeToolBarIcon(mAction); 35 | mIface->removePluginMenu(tr("&Convert to point"), mAction); 36 | delete mAction; 37 | } 38 | 39 | void QgsPointConverterPlugin::convertToPoint() 40 | { 41 | qWarning("in method convertToPoint"); 42 | QgsMapLayer* theMapLayer = mIface->activeLayer(); 43 | if(!theMapLayer) 44 | { 45 | QMessageBox::information(0, tr("no active layer"), tr("this plugin needs an active point vector layer to make conversions to points"), QMessageBox::Ok); 46 | return; 47 | } 48 | QgsVectorLayer* theVectorLayer = dynamic_cast(theMapLayer); 49 | if(!theVectorLayer) 50 | { 51 | QMessageBox::information(0, tr("no vector layer"), tr("this plugin needs an active point vector layer to make conversions to points"), QMessageBox::Ok); 52 | return; 53 | } 54 | 55 | QString fileName = QFileDialog::getSaveFileName(); 56 | if(!fileName.isNull()) 57 | { 58 | qWarning("The selected filename is: " + fileName); 59 | QFile f(fileName); 60 | if(!f.open(QIODevice::WriteOnly)) 61 | { 62 | QMessageBox::information(0, "error", "Could not open file", QMessageBox::Ok); 63 | return; 64 | } 65 | QTextStream theTextStream(&f); 66 | theTextStream.setRealNumberNotation(QTextStream::FixedNotation); 67 | 68 | theTextStream << "x,y" << endl; 69 | 70 | QgsFeature* currentFeature = 0; 71 | geos::Geometry* currentGeometry = 0; 72 | geos::CoordinateSequence* currentSequence = 0; 73 | int currentSequenceSize = 0; 74 | 75 | theVectorLayer->getDataProvider()->reset(); 76 | //first parameter: fetch attributes, second parameter: selected features only 77 | while(currentFeature = theVectorLayer->getNextFeature(true, false)) 78 | { 79 | currentGeometry = currentFeature->geosGeometry(); 80 | currentSequence = currentGeometry->getCoordinates(); 81 | currentSequenceSize = currentSequence->getSize(); 82 | for(int i = 0; i < currentSequenceSize; ++i) 83 | { 84 | theTextStream << currentSequence->getAt(i).x << "," << currentSequence->getAt(i).y << endl; 85 | } 86 | delete currentGeometry; 87 | delete currentFeature; 88 | } 89 | } 90 | } 91 | 92 | QGISEXTERN QgisPlugin* classFactory(QgisApp* app, QgisIface* iface) 93 | { 94 | return new QgsPointConverterPlugin(app, iface); 95 | } 96 | 97 | QGISEXTERN QString name() 98 | { 99 | return "point converter plugin"; 100 | } 101 | 102 | QGISEXTERN QString description() 103 | { 104 | return "A plugin that converts vector layers to delimited text point files"; 105 | } 106 | 107 | QGISEXTERN QString version() 108 | { 109 | return "0.00001"; 110 | } 111 | 112 | // Return the type (either UI or MapLayer plugin) 113 | QGISEXTERN int type() 114 | { 115 | return QgisPlugin::UI; 116 | } 117 | 118 | // Delete ourself 119 | QGISEXTERN void unload(QgisPlugin* theQgsPointConverterPluginPointer) 120 | { 121 | delete theQgsPointConverterPluginPointer; 122 | } 123 | 124 | -------------------------------------------------------------------------------- /plugin_writer_workshop/pointconverter_step3/qgspointconverterplugin.h: -------------------------------------------------------------------------------- 1 | #ifndef QGSPOINTCONVERTERPLUGIN_H 2 | #define QGSPOINTCONVERTERPLUGIN_H 3 | 4 | #include "qgisplugin.h" 5 | #include 6 | 7 | /**A plugin that converts vector layers to delimited text point files. 8 | The vertices of polygon/line type layers are converted to point features*/ 9 | class QgsPointConverterPlugin: public QObject, public QgisPlugin 10 | { 11 | Q_OBJECT 12 | 13 | public: 14 | QgsPointConverterPlugin(QgisApp* app, QgisIface* iface); 15 | ~QgsPointConverterPlugin(); 16 | void initGui(); 17 | void unload(); 18 | 19 | private: 20 | QgisIface* mIface; 21 | QAction* mAction; 22 | 23 | private slots: 24 | void convertToPoint(); 25 | }; 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /style.css: -------------------------------------------------------------------------------- 1 | body { 2 | padding: 2em 1em 2em 70px; 3 | margin: 0; 4 | font-family: sans-serif; 5 | color: black; 6 | background: white; 7 | } 8 | :link { color: #00C; background: transparent } 9 | :visited { color: #609; background: transparent } 10 | a:active { color: #C00; background: transparent } 11 | 12 | a:link img, a:visited img { border-style: none } 13 | 14 | 15 | h1, h2, h3, h4, h5, h6 { text-align: left } 16 | h1, h2, h3 { color: #005A9C; background: white } 17 | h1 { font: 170% sans-serif } 18 | h2 { font: 140% sans-serif } 19 | h3 { font: 120% sans-serif } 20 | h4 { font: bold 100% sans-serif } 21 | h5 { font: italic 100% sans-serif } 22 | h6 { font: small-caps 100% sans-serif } 23 | 24 | pre { margin-left: 2em; 25 | border: 1; 26 | padding: 4px; 27 | background: #ececec; } 28 | pre, code { font-family: monospace } 29 | 30 | 31 | --------------------------------------------------------------------------------