Python

Python

Explore python code snippets and tutorials

Python

Read srid of shapefile

Read srid of a shapefile

python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
from osgeo import ogr, osr
shapefile = ogr.Open("shapefile.shp")
prj = ogr.Open("shapefile.prj")
layer = shapefile.GetLayer(0)
crs = layer.GetSpatialRef()
prj_file = open("shapefile.prj", 'r')
prj_txt = prj_file.read()
srs = osr.SpatialReference()
srs.ImportFromESRI([prj_txt])
print('Shape prj is: %s' % prj_txt)
print('WKT is: %s' % srs.ExportToWkt())
print('Proj4 is: %s' % srs.ExportToProj4())
srs.AutoIdentifyEPSG()
print('EPSG is: %s' % srs.GetAuthorityCode(None))
Python

Breadth-first search python

Breadth-first search python

python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def bfs(graph, vertex):
    stack = []
    distances = {}
    distance = 0
    if isinstance(graph, dict):
        visited = {key: False for key in graph.keys()}
        children = graph[vertex].keys()
    if isinstance(graph, list):
        visited = [False] * len(graph)
        children = graph[vertex]
    for child in children:
        distances[child] = {
                'distance': distance + 1,
                'predecessor': vertex
            }
        stack.append(child)
    while stack:
        vertex = stack.pop()
         if not visited[vertex]:
            visited[vertex] = True
            if isinstance(graph, dict):
                children = graph[vertex].keys()
            if isinstance(graph, list):
                children = graph[vertex]
            for child in children:
                if not visited[child]:
                    distance = distances[vertex]['distance'] + 1
                    distances[child] = {
                        'distance': distance,
                        'predecessor': vertex
                    }
                    stack.append(child)
    return distances
Python

Linked List

Linked list

python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class Node:
    def __init__(self, data, next=None):
        self.data = data
        self.next = next

    def __str__(self):
        return str(self.data)

    def get_data(self):
        return self.data


class LinkedList:
    def __init__(self, head=None, counter=0):
        self.head = head
        self.counter = counter

    def prepend_node(self, data):
        node = Node(data=data)
        if not self.head:
            self.head = node
        else:
            current = self.head
            self.head = node
            self.head.next = current
        self.counter += 1

    def append_node(self, data):
        node = Node(data=data)
        if not self.head:
            self.head = node
        else:
            self.head.next = node
        self.counter += 1

    def remove_node(self, data):
        current = self.head
        while current:
            if current.data == data:
                if not current.next:
                    self.head.next = None
                else:
                    self.head = current.next
                self.counter -= 1
                return True
            current = current.next
        print('data not found')
        return False

    def traverse(self):
        current = self.head
        while current:
            yield current
            current = current.next


if __name__ == '__main__':
    linked = LinkedList()
    linked.append_node(1)
    linked.append_node(2)
    linked.prepend_node(5)
    linked.remove_node(5)
    for node in linked.traverse():
        if node:
            print(node.data)