Skip to content

Instantly share code, notes, and snippets.

View fpsampayo's full-sized avatar

Francisco Pérez Sampayo fpsampayo

View GitHub Profile
@fpsampayo
fpsampayo / pyqgis_remove_hidden_layers.md
Last active May 2, 2019 14:14
QGIS 3.x Remove hidden layers from project
project = QgsProject().instance()
root = project.layerTreeRoot()

legendLayers = root.findLayerIds()
projectLayers = project.mapLayers().keys()

layersToRemove = set(projectLayers) - set(legendLayers)

project.removeMapLayers(list(layersToRemove))
@fpsampayo
fpsampayo / pyqgis_debug.md
Created February 18, 2019 10:49
PyQgis debug with pydev

Put this code in jour plugin:

try:
    pydev_path = 'PYDEV_PATH_HERE'
    import sys;sys.path.append(pydev_path)
    import pydevd;pydevd.settrace()
except:
 print("Error activando debugger")
@fpsampayo
fpsampayo / ogr_wfs_catastro_inspire.md
Created February 15, 2018 11:40
Catastro INSPIRE desde GDAL/OGR
from osgeo import gdal, ogr, osr

url = 'http://ovc.catastro.meh.es/INSPIRE/wfsCP.aspx?service=wfs&version=2&request=getfeature&STOREDQUERIE_ID=GetParcel&srsname=EPSG:4326&REFCAT=%s'

wfs_drv = ogr.GetDriverByName('WFS')
wfs_ds = wfs_drv.Open('WFS:' + url % refcat)

layer = wfs_ds.GetLayerByName('cp:CadastralParcel')
feat = layer.GetFeature(0)
@fpsampayo
fpsampayo / postgis_mod_line_length.md
Created February 8, 2018 16:48
PostGIS - Modificación de longitud de líneas simples desde punto central

Consulta para obtener una nueva geometría de igual dirección pero de un tamaño fijo, y partiendo del punto central:

SELECT
  ST_Rotate(
  	ST_MakeLine(
	  ST_Translate(center, 0, -len / 2),
	  ST_Translate(center, 0,  len / 2)
	),
	-azm,
@fpsampayo
fpsampayo / agg_select.md
Last active December 26, 2017 16:16
Seleccionando en pyqgis por agregación
layer = iface.mapCanvas().currentLayer()

expr1 = """count("refcat", "refcat") > 1"""

exp = QgsExpression(expr1) 

exp_context = QgsExpressionContext()
exp_context.appendScope(QgsExpressionContextUtils.globalScope())
exp_context.appendScope(QgsExpressionContextUtils.projectScope())
@fpsampayo
fpsampayo / qgis_actions.md
Last active March 14, 2018 11:20
Abrir fotos desde qgis

Ejemplos de acciones en QGIS

Abrir fotos

Capa > propiedades > Acciones

Creamos nueva acción:

  • Tipo: Python
  • Descripción: Ver Foto
  • Nombre corto: Ver Foto
@fpsampayo
fpsampayo / vevn-python3-win.md
Last active July 14, 2017 12:32
Virtualenv con Python3 en Windows
@fpsampayo
fpsampayo / pyqgis.bat
Created June 12, 2017 13:31
pycharm for pyqgis development
@echo off
SET OSGEO4W_ROOT=C:\OSGeo4W64
CALL %OSGEO4W_ROOT%\bin\o4w_env.bat
@echo off
path %PATH%;%OSGEO4W_ROOT%\apps\qgis\bin
set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\qgis\python
set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\qgis\python\plugins
set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\Python27\Lib\site-packages
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT%\apps\qgis