Skip to content

Instantly share code, notes, and snippets.

Author: Sean Gillies Version: 1.0

Abstract

This document describes a GeoJSON-like protocol for geo-spatial (GIS) vector data.

Introduction

I return to this issue because it is very similar to How do I find vector line bearing in QGIS or GRASS? and it can be solved with Python in the same way:
1) Haversine distance
One can find lots of scripts by searching Haversine distance with Python on the Internet and I choose one of them in Haversine Formula in Python (Bearing and Distance between two GPS points)
def haversine(lon1, lat1, lon2, lat2):
"""
Calculate the great circle distance between two points
It is the same thing with pyshp, except that you cannot update directly the dbf file. When you read a shapefile, the data are stored in Python lists
import shapefile
input = shapefile.Reader("yourfile.shp")
shapes = input.shapes() # -> the geometries in a list
fields = input.fields[1:] -> the fields definition in a list
fields_name = = [field[0] for field in fields] -> the fields names in a list
attributes = input.records() -> the attributes in a list
If you want to use Python, you don't need QGIS, except if you want to create a plugin. In this case, you should consider PyQGIS with the reference given by Curlew
But you can also use Python modules like pyshp, osgeo (gdal and ogr) or Fiona and Shapely without QGIS
In both cases, you need a join field that will link the polygon shapefile to the point shapefile.
Example with Fiona and Shapely (all the elements of a shapefile (schema,geometry, records) are processed using Python dictionaries).
With ogr and Fiona it is easier to create a new shapefile, copying the original shapefile (geometry and attributes), and adding new fields with the desired values than modify the original shapefile.
@mlaloux
mlaloux / line_from_pt_dist2.py
Created March 7, 2013 16:58
Création d'une ligne à partir d'une direction (en degrés) et d'une distance dans QGIS - Creating a line from a direction (in degrees) and a distance in QGIS (QGIS master with the new Python API)
'''QGIS master with the new Python API
Martin Laloux 2013'''
from PyQt4.QtCore import *
from numpy import *
from PyQt4.QtCore import *
from numpy import *
a = QgsGeometry.fromPoint(QgsPoint(122.989235,13679.083853))
@mlaloux
mlaloux / line_from_pt_dist1.py
Created March 7, 2013 16:58
Création d'une ligne à partir d'une direction (en degrés) et d'une distance dans QGIS - Creating a line from a direction (in degrees) and a distance in QGIS (QGIS 2.8I)
'''For QGIS 2.8
Martin Laloux, 2012'''
from PyQt4.QtCore import *
from numpy import *
class distance(object):
def __init__(self,a,angle,distance):
self.a = a.asPoint()
self.xori = self.a[0]
@mlaloux
mlaloux / tree.md
Created March 7, 2013 17:00 — forked from hrldcpr/tree.md

One-line Tree in Python

Using Python's built-in defaultdict we can easily define a tree data structure:

def tree(): return defaultdict(tree)

That's it!

Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.