forked from domlysz/BlenderGIS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_import_georaster.py
executable file
·493 lines (442 loc) · 16.3 KB
/
io_import_georaster.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# -*- coding:utf-8 -*-
# This file is part of BlenderGIS
# ***** GPL LICENSE BLOCK *****
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# All rights reserved.
# ***** GPL LICENSE BLOCK *****
import bpy
import bmesh
import os
import math
from mathutils import Vector
import numpy as np#Ship with Blender since 2.70
import logging
log = logging.getLogger(__name__)
from ..geoscene import GeoScene, georefManagerLayout
from ..prefs import PredefCRS
from ..core.georaster import GeoRaster
from .utils import bpyGeoRaster, exportAsMesh
from .utils import placeObj, adjust3Dview, showTextures, addTexture, getBBOX
from .utils import rasterExtentToMesh, geoRastUVmap, setDisplacer
from ..core import HAS_GDAL
if HAS_GDAL:
from osgeo import gdal
from ..core import XY as xy
from ..core.errors import OverlapError
from ..core.proj import Reproj
from bpy_extras.io_utils import ImportHelper #helper class defines filename and invoke() function which calls the file selector
from bpy.props import StringProperty, BoolProperty, EnumProperty, IntProperty
from bpy.types import Operator
PKG, SUBPKG = __package__.split('.', maxsplit=1)
class IMPORTGIS_OT_georaster(Operator, ImportHelper):
"""Import georeferenced raster (need world file)"""
bl_idname = "importgis.georaster" # important since its how bpy.ops.importgis.georaster is constructed (allows calling operator from python console or another script)
#bl_idname rules: must contain one '.' (dot) charactere, no capital letters, no reserved words (like 'import')
bl_description = 'Import raster georeferenced with world file'
bl_label = "Import georaster"
bl_options = {"UNDO"}
def listObjects(self, context):
#Function used to update the objects list (obj_list) used by the dropdown box.
objs = [] #list containing tuples of each object
for index, object in enumerate(bpy.context.scene.objects): #iterate over all objects
if object.type == 'MESH':
objs.append((str(index), object.name, "Object named " +object.name)) #put each object in a tuple (key, label, tooltip) and add this to the objects list
return objs
# ImportHelper class properties
filter_glob: StringProperty(
default="*.tif;*.jpg;*.jpeg;*.png;*.bmp",
options={'HIDDEN'},
)
# Raster CRS definition
def listPredefCRS(self, context):
return PredefCRS.getEnumItems()
rastCRS: EnumProperty(
name = "Raster CRS",
description = "Choose a Coordinate Reference System",
items = listPredefCRS,
)
reprojection: BoolProperty(
name="Specifiy raster CRS",
description="Specifiy raster CRS if it's different from scene CRS",
default=False )
# List of operator properties, the attributes will be assigned
# to the class instance from the operator settings before calling.
importMode: EnumProperty(
name="Mode",
description="Select import mode",
items=[ ('PLANE', 'Basemap on new plane', "Place raster texture on new plane mesh"),
('BKG', 'Basemap as background', "Place raster as background image"),
('MESH', 'Basemap on mesh', "UV map raster on an existing mesh"),
('DEM', 'DEM as displacement texture', "Use DEM raster as height texture to wrap a base mesh"),
('DEM_RAW', 'DEM raw data build [slow]', "Import a DEM as pixels points cloud with building faces. Do not use with huge dataset.")]
)
#
objectsLst: EnumProperty(attr="obj_list", name="Objects", description="Choose object to edit", items=listObjects)
#
#Subdivise (as DEM option)
def listSubdivisionModes(self, context):
items = [ ('subsurf', 'Subsurf', "Add a subsurf modifier"), ('none', 'None', "No subdivision")]
if not self.demOnMesh:
#mesh subdivision method can not be applyed on an existing mesh
#this option makes sense only when the mesh is created from scratch
items.append(('mesh', 'Mesh', "Create vertices at each pixels"))
return items
subdivision: EnumProperty(
name="Subdivision",
description="How to subdivise the plane (dispacer needs vertex to work with)",
items=listSubdivisionModes
)
#
demOnMesh: BoolProperty(
name="Apply on existing mesh",
description="Use DEM as displacer for an existing mesh",
default=False
)
#
clip: BoolProperty(
name="Clip to working extent",
description="Use the reference bounding box to clip the DEM",
default=False
)
#
demInterpolation: BoolProperty(
name="Smooth relief",
description="Use texture interpolation to smooth the resulting terrain",
default=True
)
#
fillNodata: BoolProperty(
name="Fill nodata values",
description="Interpolate existing nodata values to get an usuable displacement texture",
default=False
)
#
step: IntProperty(name = "Step", default=1, description="Pixel step", min=1)
buildFaces: BoolProperty(name="Build faces", default=True, description='Build quad faces connecting pixel point cloud')
def draw(self, context):
#Function used by blender to draw the panel.
layout = self.layout
layout.prop(self, 'importMode')
scn = bpy.context.scene
geoscn = GeoScene(scn)
#
if self.importMode == 'PLANE':
pass
#
if self.importMode == 'BKG':
pass
#
if self.importMode == 'MESH':
if geoscn.isGeoref and len(self.objectsLst) > 0:
layout.prop(self, 'objectsLst')
else:
layout.label(text="There isn't georef mesh to UVmap on")
#
if self.importMode == 'DEM':
layout.prop(self, 'demOnMesh')
if self.demOnMesh:
if geoscn.isGeoref and len(self.objectsLst) > 0:
layout.prop(self, 'objectsLst')
layout.prop(self, 'clip')
else:
layout.label(text="There isn't georef mesh to apply on")
layout.prop(self, 'subdivision')
layout.prop(self, 'demInterpolation')
if self.subdivision == 'mesh':
layout.prop(self, 'step')
layout.prop(self, 'fillNodata')
#
if self.importMode == 'DEM_RAW':
layout.prop(self, 'buildFaces')
layout.prop(self, 'step')
layout.prop(self, 'clip')
if self.clip:
if geoscn.isGeoref and len(self.objectsLst) > 0:
layout.prop(self, 'objectsLst')
else:
layout.label(text="There isn't georef mesh to refer")
#
if geoscn.isPartiallyGeoref:
layout.prop(self, 'reprojection')
if self.reprojection:
self.crsInputLayout(context)
#
georefManagerLayout(self, context)
else:
self.crsInputLayout(context)
def crsInputLayout(self, context):
layout = self.layout
row = layout.row(align=True)
split = row.split(factor=0.35, align=True)
split.label(text='CRS:')
split.prop(self, "rastCRS", text='')
row.operator("bgis.add_predef_crs", text='', icon='ADD')
@classmethod
def poll(cls, context):
return context.mode == 'OBJECT'
def execute(self, context):
prefs = context.preferences.addons[PKG].preferences
bpy.ops.object.select_all(action='DESELECT')
#Get scene and some georef data
scn = bpy.context.scene
geoscn = GeoScene(scn)
if geoscn.isBroken:
self.report({'ERROR'}, "Scene georef is broken, please fix it beforehand")
return {'CANCELLED'}
scale = geoscn.scale #TODO
if geoscn.isGeoref:
dx, dy = geoscn.getOriginPrj()
if self.reprojection:
rastCRS = self.rastCRS
else:
rastCRS = geoscn.crs
else: #if not geoscn.hasCRS
rastCRS = self.rastCRS
try:
geoscn.crs = rastCRS
except Exception as e:
log.error("Cannot set scene crs", exc_info=True)
self.report({'ERROR'}, "Cannot set scene crs, check logs for more infos")
return {'CANCELLED'}
#Raster reprojection throught UV mapping
#build reprojector objects
if geoscn.crs != rastCRS:
rprj = True
rprjToRaster = Reproj(geoscn.crs, rastCRS)
rprjToScene = Reproj(rastCRS, geoscn.crs)
else:
rprj = False
rprjToRaster = None
rprjToScene = None
#Path
filePath = self.filepath
name = os.path.basename(filePath)[:-4]
######################################
if self.importMode == 'PLANE':#on plane
#Load raster
try:
rast = bpyGeoRaster(filePath)
except IOError as e:
log.error("Unable to open raster", exc_info=True)
self.report({'ERROR'}, "Unable to open raster, check logs for more infos")
return {'CANCELLED'}
#Get or set georef dx, dy
if not geoscn.isGeoref:
dx, dy = rast.center.x, rast.center.y
if rprj:
dx, dy = rprjToScene.pt(dx, dy)
geoscn.setOriginPrj(dx, dy)
#create a new mesh from raster extent
mesh = rasterExtentToMesh(name, rast, dx, dy, reproj=rprjToScene)
#place obj
obj = placeObj(mesh, name)
#UV mapping
uvTxtLayer = mesh.uv_layers.new(name='rastUVmap')# Add UV map texture layer
geoRastUVmap(obj, uvTxtLayer, rast, dx, dy, reproj=rprjToRaster)
# Create material
mat = bpy.data.materials.new('rastMat')
# Add material to current object
obj.data.materials.append(mat)
# Add texture to material
addTexture(mat, rast.bpyImg, uvTxtLayer, name='rastText')
######################################
if self.importMode == 'BKG':#background
if rprj:
#TODO, do gdal true reproj
self.report({'ERROR'}, "Raster reprojection is not possible in background mode")
return {'CANCELLED'}
#Load raster
try:
rast = bpyGeoRaster(filePath)
except IOError as e:
log.error("Unable to open raster", exc_info=True)
self.report({'ERROR'}, "Unable to open raster, check logs for more infos")
return {'CANCELLED'}
#Check pixel size and rotation
if rast.rotation.xy != [0,0]:
self.report({'ERROR'}, "Cannot apply a rotation in background image mode")
return {'CANCELLED'}
if abs(round(rast.pxSize.x, 3)) != abs(round(rast.pxSize.y, 3)):
self.report({'ERROR'}, "Background image needs equal pixel size in map units in both x ans y axis")
return {'CANCELLED'}
#
trueSizeX = rast.geoSize.x
trueSizeY = rast.geoSize.y
ratio = rast.size.x / rast.size.y
if geoscn.isGeoref:
offx, offy = rast.center.x - dx, rast.center.y - dy
else:
dx, dy = rast.center.x, rast.center.y
geoscn.setOriginPrj(dx, dy)
offx, offy = 0, 0
bkg = bpy.data.objects.new(self.name, None) #None will create an empty
bkg.empty_display_type = 'IMAGE'
bkg.empty_image_depth = 'BACK'
bkg.data = rast.bpyImg
scn.collection.objects.link(bkg)
bkg.empty_display_size = 1 #a size of 1 means image width=1bu
bkg.scale = (trueSizeX, trueSizeY*ratio, 1)
bkg.location = (offx, offy, 0)
bpy.context.view_layer.objects.active = bkg
bkg.select_set(True)
if prefs.adjust3Dview:
adjust3Dview(context, rast.bbox)
######################################
if self.importMode == 'MESH':
if not geoscn.isGeoref or len(self.objectsLst) == 0:
self.report({'ERROR'}, "There isn't georef mesh to apply on")
return {'CANCELLED'}
# Get choosen object
obj = scn.objects[int(self.objectsLst)]
# Select and active this obj
obj.select_set(True)
context.view_layer.objects.active = obj
# Compute projeted bbox (in geographic coordinates system)
subBox = getBBOX.fromObj(obj).toGeo(geoscn)
if rprj:
subBox = rprjToRaster.bbox(subBox)
#Load raster
try:
rast = bpyGeoRaster(filePath, subBoxGeo=subBox)
except IOError as e:
log.error("Unable to open raster", exc_info=True)
self.report({'ERROR'}, "Unable to open raster, check logs for more infos")
return {'CANCELLED'}
except OverlapError:
self.report({'ERROR'}, "Non overlap data")
return {'CANCELLED'}
# Add UV map texture layer
mesh = obj.data
uvTxtLayer = mesh.uv_layers.new(name='rastUVmap')
uvTxtLayer.active = True
# UV mapping
geoRastUVmap(obj, uvTxtLayer, rast, dx, dy, reproj=rprjToRaster)
# Add material and texture
mat = bpy.data.materials.new('rastMat')
obj.data.materials.append(mat)
addTexture(mat, rast.bpyImg, uvTxtLayer, name='rastText')
######################################
if self.importMode == 'DEM':
# Get reference plane
if self.demOnMesh:
if not geoscn.isGeoref or len(self.objectsLst) == 0:
self.report({'ERROR'}, "There isn't georef mesh to apply on")
return {'CANCELLED'}
# Get choosen object
obj = scn.objects[int(self.objectsLst)]
mesh = obj.data
# Select and active this obj
obj.select_set(True)
context.view_layer.objects.active = obj
# Compute projeted bbox (in geographic coordinates system)
subBox = getBBOX.fromObj(obj).toGeo(geoscn)
if rprj:
subBox = rprjToRaster.bbox(subBox)
else:
subBox = None
# Load raster
try:
grid = bpyGeoRaster(filePath, subBoxGeo=subBox, clip=self.clip, fillNodata=self.fillNodata, useGDAL=HAS_GDAL, raw=True)
except IOError as e:
log.error("Unable to open raster", exc_info=True)
self.report({'ERROR'}, "Unable to open raster, check logs for more infos")
return {'CANCELLED'}
except OverlapError:
self.report({'ERROR'}, "Non overlap data")
return {'CANCELLED'}
# If needed, create a new plane object from raster extent
if not self.demOnMesh:
if not geoscn.isGeoref:
dx, dy = grid.center.x, grid.center.y
if rprj:
dx, dy = rprjToScene.pt(dx, dy)
geoscn.setOriginPrj(dx, dy)
if self.subdivision == 'mesh':#Mesh cut
mesh = exportAsMesh(grid, dx, dy, self.step, reproj=rprjToScene, flat=True)
else:
mesh = rasterExtentToMesh(name, grid, dx, dy, pxLoc='CENTER', reproj=rprjToScene) #use pixel center to avoid displacement glitch
obj = placeObj(mesh, name)
subBox = getBBOX.fromObj(obj).toGeo(geoscn)
# Add UV map texture layer
previousUVmapIdx = mesh.uv_layers.active_index
uvTxtLayer = mesh.uv_layers.new(name='demUVmap')
#UV mapping
geoRastUVmap(obj, uvTxtLayer, grid, dx, dy, reproj=rprjToRaster)
#Restore previous uv map
if previousUVmapIdx != -1:
mesh.uv_layers.active_index = previousUVmapIdx
#Make subdivision
if self.subdivision == 'subsurf':#Add subsurf modifier
if not 'SUBSURF' in [mod.type for mod in obj.modifiers]:
subsurf = obj.modifiers.new('DEM', type='SUBSURF')
subsurf.subdivision_type = 'SIMPLE'
subsurf.levels = 6
subsurf.render_levels = 6
#Set displacer
dsp = setDisplacer(obj, grid, uvTxtLayer, interpolation=self.demInterpolation)
######################################
if self.importMode == 'DEM_RAW':
# Get reference plane
subBox = None
if self.clip:
if not geoscn.isGeoref or len(self.objectsLst) == 0:
self.report({'ERROR'}, "No working extent")
return {'CANCELLED'}
# Get choosen object
obj = scn.objects[int(self.objectsLst)]
subBox = getBBOX.fromObj(obj).toGeo(geoscn)
if rprj:
subBox = rprjToRaster.bbox(subBox)
# Load raster
try:
grid = GeoRaster(filePath, subBoxGeo=subBox, useGDAL=HAS_GDAL)
except IOError as e:
log.error("Unable to open raster", exc_info=True)
self.report({'ERROR'}, "Unable to open raster, check logs for more infos")
return {'CANCELLED'}
except OverlapError:
self.report({'ERROR'}, "Non overlap data")
return {'CANCELLED'}
if not geoscn.isGeoref:
dx, dy = grid.center.x, grid.center.y
if rprj:
dx, dy = rprjToScene.pt(dx, dy)
geoscn.setOriginPrj(dx, dy)
mesh = exportAsMesh(grid, dx, dy, self.step, reproj=rprjToScene, subset=self.clip, flat=False, buildFaces=self.buildFaces)
obj = placeObj(mesh, name)
#grid.unload()
######################################
#Flag if a new object as been created...
if self.importMode == 'PLANE' or (self.importMode == 'DEM' and not self.demOnMesh) or self.importMode == 'DEM_RAW':
newObjCreated = True
else:
newObjCreated = False
#...if so, maybee we need to adjust 3d view settings to it
if newObjCreated and prefs.adjust3Dview:
bb = getBBOX.fromObj(obj)
adjust3Dview(context, bb)
#Force view mode with textures
if prefs.forceTexturedSolid:
showTextures(context)
return {'FINISHED'}
def register():
try:
bpy.utils.register_class(IMPORTGIS_OT_georaster)
except ValueError as e:
log.warning('{} is already registered, now unregister and retry... '.format(IMPORTGIS_OT_georaster))
unregister()
bpy.utils.register_class(IMPORTGIS_OT_georaster)
def unregister():
bpy.utils.unregister_class(IMPORTGIS_OT_georaster)