Example #1
0
   def render(self, layers, styles, bounds, size, **options):
      self.map = DefaultMapContext(bounds.proj._crs)
      self.map.setAreaOfInterest(bounds)

      for i in range(len(layers)): 
        l = layers[i] 
        data = l._coverage if isinstance(l,Raster) else l._source
        self.map.addLayer(DefaultMapLayer(data, styles[i]._style()))

      w,h = (size[0], size[1]) 

      hints = {}
      hints [awt.RenderingHints.KEY_ANTIALIASING] = awt.RenderingHints.VALUE_ANTIALIAS_ON
      
      renderer = StreamingRenderer()
      renderer.setJava2DHints(awt.RenderingHints(hints))
      renderer.setContext(self.map)

      img = image.BufferedImage(w, h, image.BufferedImage.TYPE_INT_ARGB)
      g = img.getGraphics()
      g.setColor(style.Color(options["bgcolor"]).expr.value if options.has_key("bgcolor") else awt.Color.white)
      g.fillRect(0, 0, w, h)
      
      renderer.paint(g, awt.Rectangle(w,h), bounds)
      return self._encode(img, g, size, **options)   
Example #2
0
   def render(self, layers, styles, bounds, size, **options):
      self.map = DefaultMapContext(bounds.proj._crs)
      self.map.setAreaOfInterest(bounds)

      for i in range(len(layers)):
        self.map.addLayer(DefaultMapLayer(layers[i]._source,styles[i]._style()))

      w,h = (size[0], size[1]) 

      hints = {}
      hints [awt.RenderingHints.KEY_ANTIALIASING] = awt.RenderingHints.VALUE_ANTIALIAS_ON
      
      renderer = StreamingRenderer()
      renderer.java2DHints = awt.RenderingHints(hints)

      mappane = JMapPane(self.map)
      mappane.renderer = renderer
      mappane.size = (w,h) 
      mappane.visible = True

      f = Frame(mappane)
      f.setSize(w,h)
      f.setVisible(True)
Example #3
0
    def renderMap(self, connection_parameters=None, sql=None, geom_id_entity=None, geom_entity=None, data_entity=None, map_parameters={}):

        # Put connection parameters into a java HashMap.
        params_hashmap = HashMap()
        for param, value in connection_parameters.items():
            if value:
                params_hashmap.put(param, value)

        # Get data store.
        data_store = DataStoreFinder.getDataStore(params_hashmap)

        # Create VirtualTable from sql.
        vtable = VirtualTable("vtable", sql)

        # Set primary key.
        vtable.setPrimaryKeyColumns([geom_id_entity['ID']])

        # metadatata = intententional typo. GT needs to fix the name.
        vtable.addGeometryMetadatata(geom_entity['ID'], JPolygon, 4326)

        # Create feature source from virtual table.
        data_store.addVirtualTable(vtable)
        feature_source = data_store.getFeatureSource("vtable")

        # Add styling classes if there was a value entity.
        if data_entity:
            # Generate class bounds.
            num_classes = data_entity.get('num_classes', 25)
            vmin = float(data_entity.get('min', 0))
            vmax = float(data_entity.get('max', 1))
            vrange = vmax - vmin
            class_width = vrange/num_classes
            classes = [(None, vmin)]
            for i in range(num_classes):
                classes.append((vmin + i * class_width, vmin + (i + 1) * class_width))
            classes.append((vmax, None))

            # Generate style rules for classes.
            rules = []
            for c in classes:
                rule = self.create_rule(c[0], c[1], vmin, vrange, attr=data_entity['ID'])
                rules.append(rule)
            feature_type_style = self.style_factory.createFeatureTypeStyle(rules)
            style = self.style_factory.createStyle()
            style.featureTypeStyles().add(feature_type_style)
        else:
            style = None

        # Setup map.
        gt_map = DefaultMapContext()
        gt_map.addLayer(feature_source, style)
        gt_renderer = StreamingRenderer()
        gt_renderer.setMapContent(gt_map)
        image_bounds = Rectangle(0, 0, map_parameters.get('WIDTH', 100), map_parameters.get('HEIGHT', 100))

        # Set image type based on format.
        image_format = map_parameters.get('FORMAT', 'image/png')
        if image_format == 'image/jpeg':
            image_type = BufferedImage.TYPE_INT_RGB
        else:
            image_type = BufferedImage.TYPE_INT_ARGB

        buffered_image = BufferedImage(image_bounds.width, image_bounds.height, image_type)
        graphics = buffered_image.createGraphics()

        # Set background color if not transparent.
        if not map_parameters.get('TRANSPARENT'):
            graphics.setPaint(Color.WHITE)
            graphics.fill(image_bounds)

        crs = CRS.decode(map_parameters.get('SRS', "EPSG:4326"))
        bbox = map_parameters.get('BBOX', '-180,-90,180,90')
        coords = [float(coord) for coord in bbox.split(",")]
        map_bounds = ReferencedEnvelope(coords[0], coords[2], coords[1], coords[3], crs)

        gt_renderer.paint(graphics, image_bounds, map_bounds)

        # Release the JDBC connection and map content.
        data_store.dispose()
        gt_renderer.getMapContent().dispose()

        # Return raw image.
        byte_array_output_stream = ByteArrayOutputStream()
        informal_format = re.match('image/(.*)', image_format).group(1)
        ImageIO.write(buffered_image, informal_format, byte_array_output_stream)
        byte_array = byte_array_output_stream.toByteArray()
        raw_image = Py.newString(StringUtil.fromBytes(byte_array))
        return raw_image