-
Notifications
You must be signed in to change notification settings - Fork 23
/
pyEarth.py
142 lines (125 loc) · 5.11 KB
/
pyEarth.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import pyproj
import shapefile
import shapely.geometry
import sys
from math import cos, pi, sin
from OpenGL.GL import *
from OpenGL.GLU import *
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtWidgets import (QAction, QApplication, QFileDialog, QGridLayout,
QMainWindow, QWidget, QOpenGLWidget)
class View(QOpenGLWidget):
def __init__(self, parent=None):
super().__init__(parent)
for coord in ('x', 'y', 'z', 'cx', 'cy', 'cz', 'rx', 'ry', 'rz'):
setattr(self, coord, 50 if coord == 'z' else 0)
self.timer = QTimer(self)
self.timer.timeout.connect(self.rotate)
def initializeGL(self):
glMatrixMode(GL_PROJECTION)
self.create_polygons()
glFrustum(-1, 1, -1, 1, 5, 1000)
def paintGL(self):
glColor(0, 0, 255)
glEnable(GL_DEPTH_TEST)
glBegin(GL_POLYGON)
for vertex in range(0, 100):
angle, radius = float(vertex)*2.0*pi/100, 6.370997
glVertex3f(cos(angle)*radius, sin(angle)*radius, 0.0)
glEnd()
if hasattr(self, 'polygons'):
glPushMatrix()
glRotated(self.rx/16, 1, 0, 0)
glRotated(self.ry/16, 0, 1, 0)
glRotated(self.rz/16, 0, 0, 1)
glCallList(self.polygons)
glPopMatrix()
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
gluLookAt(self.x, self.y, self.z, self.cx, self.cy, self.cz, 0, 1, 0)
self.update()
def mousePressEvent(self, event):
self.last_pos = event.pos()
def wheelEvent(self, event):
self.z += -2 if event.angleDelta().y() > 0 else 2
def mouseMoveEvent(self, event):
dx, dy = event.x() - self.last_pos.x(), event.y() - self.last_pos.y()
if event.buttons() == Qt.LeftButton:
self.rx, self.ry = self.rx + 8*dy, self.ry + 8*dx
elif event.buttons() == Qt.RightButton:
self.cx, self.cy = self.cx - dx/50, self.cy + dy/50
self.last_pos = event.pos()
def keyPressEvent(self, event):
if event.key() == Qt.Key_Space:
if self.timer.isActive():
self.timer.stop()
else:
self.timer.start()
def rotate(self):
self.rx, self.ry = self.rx + 6, self.ry + 6
def create_polygons(self):
self.polygons = glGenLists(1)
glNewList(self.polygons, GL_COMPILE)
for polygon in self.extract_polygons():
glLineWidth(2)
glBegin(GL_LINE_LOOP)
glColor(0, 0, 0)
for lon, lat in polygon.exterior.coords:
glVertex3f(*self.LLH_to_ECEF(lat, lon, 1))
glEnd()
glColor(0, 255, 0)
glBegin(GL_TRIANGLES)
for vertex in self.polygon_tesselator(polygon):
glVertex(*vertex)
glEnd()
glEndList()
def polygon_tesselator(self, polygon):
vertices, tess = [], gluNewTess()
gluTessCallback(tess, GLU_TESS_EDGE_FLAG_DATA, lambda *args: None)
gluTessCallback(tess, GLU_TESS_VERTEX, lambda v: vertices.append(v))
gluTessCallback(tess, GLU_TESS_COMBINE, lambda v, *args: v)
gluTessCallback(tess, GLU_TESS_END, lambda: None)
gluTessBeginPolygon(tess, 0)
gluTessBeginContour(tess)
for lon, lat in polygon.exterior.coords:
point = self.LLH_to_ECEF(lat, lon, 0)
gluTessVertex(tess, point, point)
gluTessEndContour(tess)
gluTessEndPolygon(tess)
gluDeleteTess(tess)
return vertices
def extract_polygons(self):
if not hasattr(self, 'shapefile'):
return
sf = shapefile.Reader(self.shapefile)
polygons = sf.shapes()
for polygon in polygons:
polygon = shapely.geometry.shape(polygon)
yield from [polygon] if polygon.geom_type == 'Polygon' else polygon
def LLH_to_ECEF(self, lat, lon, alt):
ecef, llh = pyproj.Proj(proj='geocent'), pyproj.Proj(proj='latlong')
x, y, z = pyproj.transform(llh, ecef, lon, lat, alt, radians=False)
return x/1000000, y/1000000, z/1000000
class PyEarth(QMainWindow):
def __init__(self):
super().__init__()
central_widget = QWidget(self)
self.setCentralWidget(central_widget)
menu_bar = self.menuBar()
import_shapefile = QAction('Import shapefile', self)
import_shapefile.triggered.connect(self.import_shapefile)
menu_bar.addAction(import_shapefile)
self.view = View()
self.view.setFocusPolicy(Qt.StrongFocus)
layout = QGridLayout(central_widget)
layout.addWidget(self.view, 0, 0)
def import_shapefile(self):
self.view.shapefile = QFileDialog.getOpenFileName(self, 'Import')[0]
self.view.create_polygons()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = PyEarth()
window.setWindowTitle('pyEarth: a lightweight 3D visualization of the earth')
window.setFixedSize(900, 900)
window.show()
sys.exit(app.exec_())