Exemplo n.º 1
0
    def __init__(self, settings, objectTypeManager, multiple_files=False):
        JSWriter.__init__(self, settings.path_root, multiple_files)

        self.settings = settings
        self.demProvider = settings.demProvider()
        self.objectTypeManager = objectTypeManager

        self.layerCount = 0
        self.currentLayerIndex = 0
        self.currentFeatureIndex = -1
        self.attrs = []
        self.imageManager = ImageManager(settings)
        self.modelManager = ModelManager()
        self.triMesh = {}
Exemplo n.º 2
0
  def __init__(self, settings, objectTypeManager, multiple_files=False):
    JSWriter.__init__(self, settings.path_root, multiple_files)

    self.settings = settings
    self.demProvider = settings.demProvider
    self.objectTypeManager = objectTypeManager

    self.layerCount = 0
    self.currentLayerIndex = 0
    self.currentFeatureIndex = -1
    self.attrs = []
    self.imageManager = ImageManager(settings)
    self.modelManager = ModelManager()
    self.triMesh = {}
Exemplo n.º 3
0
  def __init__(self, settings, objectTypeManager, multiple_files=False):
    JSWriter.__init__(self, settings.path_root, multiple_files)

    self.settings = settings
    self.objectTypeManager = objectTypeManager

    self.layerCount = 0
    self.currentLayerIndex = 0
    self.currentFeatureIndex = -1
    self.attrs = []

    if settings.demLayer:
      self.warp_dem = MemoryWarpRaster(settings.demLayer.source(), str(settings.demLayer.crs().toWkt()), str(settings.crs.toWkt()))
    else:
      self.warp_dem = FlatRaster()

    self.imageManager = ImageManager(settings)
    self.modelManager = ModelManager()
    self.triMesh = {}
Exemplo n.º 4
0
class ThreejsJSWriter(JSWriter):

  def __init__(self, settings, objectTypeManager, multiple_files=False):
    JSWriter.__init__(self, settings.path_root, multiple_files)

    self.settings = settings
    self.objectTypeManager = objectTypeManager

    self.layerCount = 0
    self.currentLayerIndex = 0
    self.currentFeatureIndex = -1
    self.attrs = []

    if settings.demLayer:
      self.warp_dem = MemoryWarpRaster(settings.demLayer.source(), str(settings.demLayer.crs().toWkt()), str(settings.crs.toWkt()))
    else:
      self.warp_dem = FlatRaster()

    self.imageManager = ImageManager(settings)
    self.modelManager = ModelManager()
    self.triMesh = {}

  def writeProject(self):
    # write project information
    self.write(u"// Qgis2threejs Project\n")
    settings = self.settings
    extent = self.settings.baseExtent
    rect = extent.unrotatedRect()
    mapTo3d = self.settings.mapTo3d
    wgs84Center = self.settings.wgs84Center

    args = {"title": settings.title,
            "crs": unicode(settings.crs.authid()),
            "proj": settings.crs.toProj4(),
            "baseExtent": [rect.xMinimum(), rect.yMinimum(), rect.xMaximum(), rect.yMaximum()],
            "rotation": extent.rotation(),
            "width": mapTo3d.planeWidth,
            "zExaggeration": mapTo3d.verticalExaggeration,
            "zShift": mapTo3d.verticalShift,
            "wgs84Center": {"lat": wgs84Center.y(), "lon": wgs84Center.x()}}

    self.write(u"project = new Q3D.Project({0});\n".format(pyobj2js(args)))

  def writeLayer(self, obj, fieldNames=None):
    self.currentLayerIndex = self.layerCount
    type2classprefix = {"dem": "DEM", "point": "Point", "line": "Line", "polygon": "Polygon"}
    self.write(u"\n// Layer {0}\n".format(self.currentLayerIndex))
    self.write(u"lyr = project.addLayer(new Q3D.{0}Layer({1}));\n".format(type2classprefix[obj["type"]], pyobj2js(obj)))
    # del obj["type"]

    if fieldNames is not None:
      self.write(u"lyr.a = {0};\n".format(pyobj2js(fieldNames)))
    self.layerCount += 1
    self.currentFeatureIndex = -1
    self.attrs = []
    return self.currentLayerIndex

  def writeFeature(self, f):
    self.currentFeatureIndex += 1
    self.write(u"lyr.f[{0}] = {1};\n".format(self.currentFeatureIndex, pyobj2js(f)))

  def addAttributes(self, attrs):
    self.attrs.append(attrs)

  def writeAttributes(self):
    for index, attrs in enumerate(self.attrs):
      self.write(u"lyr.f[{0}].a = {1};\n".format(index, pyobj2js(attrs, True)))

  def writeMaterials(self, materialManager):
    materialManager.write(self, self.imageManager)

  def writeImages(self):
    self.imageManager.write(self)

  def writeModelData(self):
    self.modelManager.write(self)

  def filesToCopy(self):
    # three.js library
    files = [{"dirs": ["js/threejs"]}]

    #TODO: if not export_mode:
    # controls
    files.append({"files": ["js/threejs/controls/" + self.settings.controls], "dest": "threejs"})

    # template specific libraries (files)
    config = self.settings.templateConfig

    for f in config.get("files", "").strip().split(","):
      p = f.split(">")
      fs = {"files": [p[0]]}
      if len(p) > 1:
        fs["dest"] = p[1]
      files.append(fs)

    for d in config.get("dirs", "").strip().split(","):
      p = d.split(">")
      ds = {"dirs": [p[0]], "subdirs": True}
      if len(p) > 1:
        ds["dest"] = p[1]
      files.append(ds)

    # proj4js
    if self.settings.coordsInWGS84:
      files.append({"dirs": ["js/proj4js"]})

    # model importer
    files += self.modelManager.filesToCopy()

    return files

  def scripts(self):
    files = self.modelManager.scripts()

    # proj4.js
    if self.settings.coordsInWGS84:    # display coordinates in latitude and longitude
      files.append("proj4js/proj4.js")

    # data files
    filetitle = self.settings.htmlfiletitle
    if self.multiple_files:
      files += map(lambda x: "%s_%s.js" % (filetitle, x), range(self.jsfile_count))
    else:
      files.append("%s.js" % filetitle)

    return map(lambda fn: '<script src="./%s"></script>' % fn, files)

  def triangleMesh(self, dem_width=0, dem_height=0):
    if dem_width == 0 and dem_height == 0:
      prop = DEMPropertyReader(self.settings.get(ObjectTreeItem.ITEM_DEM))
      dem_width = prop.width()
      dem_height = prop.height()

    key = "{0}x{1}".format(dem_width, dem_height)
    if key not in self.triMesh:
      mapTo3d = self.settings.mapTo3d
      hw = 0.5 * mapTo3d.planeWidth
      hh = 0.5 * mapTo3d.planeHeight
      self.triMesh[key] = TriangleMesh(-hw, -hh, hw, hh, dem_width - 1, dem_height - 1)
    return self.triMesh[key]

  def log(self, message):
    QgsMessageLog.logMessage(message, "Qgis2threejs")
Exemplo n.º 5
0
class ThreejsJSWriter(JSWriter):
    def __init__(self, settings, objectTypeManager, multiple_files=False):
        JSWriter.__init__(self, settings.path_root, multiple_files)

        self.settings = settings
        self.demProvider = settings.demProvider()
        self.objectTypeManager = objectTypeManager

        self.layerCount = 0
        self.currentLayerIndex = 0
        self.currentFeatureIndex = -1
        self.attrs = []
        self.imageManager = ImageManager(settings)
        self.modelManager = ModelManager()
        self.triMesh = {}

    def writeProject(self):
        # write project information
        self.write(u"// PhyloGeoRec Project\n")
        settings = self.settings
        extent = self.settings.baseExtent
        rect = extent.unrotatedRect()
        mapTo3d = self.settings.mapTo3d()
        wgs84Center = self.settings.wgs84Center()

        args = {
            "title":
            settings.title,
            "crs":
            unicode(settings.crs.authid()),
            "proj":
            settings.crs.toProj4(),
            "baseExtent": [
                rect.xMinimum(),
                rect.yMinimum(),
                rect.xMaximum(),
                rect.yMaximum()
            ],
            "rotation":
            extent.rotation(),
            "width":
            mapTo3d.planeWidth,
            "zExaggeration":
            mapTo3d.verticalExaggeration,
            "zShift":
            mapTo3d.verticalShift,
            "wgs84Center": {
                "lat": wgs84Center.y(),
                "lon": wgs84Center.x()
            }
        }

        self.write(u"project = new Q3D.Project({0});\n".format(pyobj2js(args)))

    def writeLayer(self, obj, fieldNames=None):
        self.currentLayerIndex = self.layerCount
        type2classprefix = {
            "dem": "DEM",
            "point": "Point",
            "line": "Line",
            "polygon": "Polygon"
        }
        self.write(u"\n// Layer {0}\n".format(self.currentLayerIndex))
        self.write(u"lyr = project.addLayer(new Q3D.{0}Layer({1}));\n".format(
            type2classprefix[obj["type"]], pyobj2js(obj)))
        # del obj["type"]

        if fieldNames is not None:
            self.write(u"lyr.a = {0};\n".format(pyobj2js(fieldNames)))
        self.layerCount += 1
        self.currentFeatureIndex = -1
        self.attrs = []
        return self.currentLayerIndex

    def writeFeature(self, f):
        self.currentFeatureIndex += 1
        self.write(u"lyr.f[{0}] = {1};\n".format(self.currentFeatureIndex,
                                                 pyobj2js(f)))

    def addAttributes(self, attrs):
        self.attrs.append(attrs)

    def writeAttributes(self):
        for index, attrs in enumerate(self.attrs):
            self.write(u"lyr.f[{0}].a = {1};\n".format(index,
                                                       pyobj2js(attrs, True)))

    def writeMaterials(self, materialManager):
        materialManager.write(self, self.imageManager)

    def writeImages(self):
        self.imageManager.write(self)

    def writeModelData(self):
        self.modelManager.write(self)

    def filesToCopy(self):
        # three.js library
        files = [{"dirs": ["js/threejs"]}]

        #TODO: if not export_mode:
        # controls
        files.append({
            "files": ["js/threejs/controls/" + self.settings.controls],
            "dest": "threejs"
        })

        # template specific libraries (files)
        config = self.settings.templateConfig()

        for f in config.get("files", "").strip().split(","):
            p = f.split(">")
            fs = {"files": [p[0]]}
            if len(p) > 1:
                fs["dest"] = p[1]
            files.append(fs)

        for d in config.get("dirs", "").strip().split(","):
            p = d.split(">")
            ds = {"dirs": [p[0]], "subdirs": True}
            if len(p) > 1:
                ds["dest"] = p[1]
            files.append(ds)

        # proj4js
        if self.settings.coordsInWGS84:
            files.append({"dirs": ["js/proj4js"]})

        # model importer
        files += self.modelManager.filesToCopy()

        return files

    def scripts(self):
        files = self.modelManager.scripts()

        # proj4.js
        if self.settings.coordsInWGS84:  # display coordinates in latitude and longitude
            files.append("proj4js/proj4.js")

        # data files
        filetitle = self.settings.htmlfiletitle
        if self.multiple_files:
            files += map(lambda x: "%s_%s.js" % (filetitle, x),
                         range(self.jsfile_count))
        else:
            files.append("%s.js" % filetitle)

        return map(lambda fn: '<script src="./%s"></script>' % fn, files)

    def triangleMesh(self, dem_width=0, dem_height=0):
        if dem_width == 0 and dem_height == 0:
            prop = DEMPropertyReader(self.settings.get(
                ObjectTreeItem.ITEM_DEM))
            dem_size = prop.demSize(self.settings.mapSettings.outputSize())
            dem_width = dem_size.width()
            dem_height = dem_size.height()

        key = "{0}x{1}".format(dem_width, dem_height)
        if key not in self.triMesh:
            mapTo3d = self.settings.mapTo3d()
            hw = 0.5 * mapTo3d.planeWidth
            hh = 0.5 * mapTo3d.planeHeight
            self.triMesh[key] = TriangleMesh(-hw, -hh, hw, hh, dem_width - 1,
                                             dem_height - 1)
        return self.triMesh[key]

    def log(self, message):
        logMessage(message)