예제 #1
0
파일: map.go 프로젝트: GeoNet/mtr
func layerGetter(ctx groupcache.Context, key string, dest groupcache.Sink) error {
	m, err := fromKey(key)
	if err != nil {
		return err
	}

	land, err := m.nePolySVG(m.zoom, 0)
	if err != nil {
		return err
	}

	lakes, err := m.nePolySVG(m.zoom, 1)
	if err != nil {
		return err
	}

	l, err := m.labels()
	if err != nil {
		return err
	}

	var b bytes.Buffer

	b.WriteString(fmt.Sprintf("<path fill=\"whitesmoke\" stroke-width=\"1\"  stroke-linejoin=\"round\" stroke=\"lightslategrey\" d=\"%s\"/>", land))
	b.WriteString(fmt.Sprintf("<path fill=\"azure\" stroke-width=\"1\"  stroke=\"lightslategrey\" d=\"%s\"/>", lakes))
	b.WriteString(labelsToSVG(l))

	return dest.SetString(b.String())
}
예제 #2
0
// Get implements Getter.
func (gt *Getter) Get(ctx groupcache.Context, key string, dest groupcache.Sink) error {
	myctx, ok := ctx.(*Context)
	if !ok {
		return fmt.Errorf("invalid context type: %T", ctx)
	}
	if myctx == nil {
		return fmt.Errorf("context is nil")
	}
	if myctx.Params == nil {
		return fmt.Errorf("context has nil Params")
	}
	im, err := gt.Server.Get(myctx.Params)
	if err != nil {
		return err
	}
	data, err := im.MarshalBinary()
	if err != nil {
		return err
	}
	err = dest.SetBytes(data)
	if err != nil {
		return err
	}
	return nil
}
예제 #3
0
func radosGetter(ctx groupcache.Context, key string, dest groupcache.Sink) error {
	rr := ctx.(*RadosReaderAt)

	k := &Key{}
	err := k.FromString(key)
	if err != nil {
		return err
	}

	buf := chunkPool.Get().([]byte)
	defer chunkPool.Put(buf)
	readP := buf
	readN := 0
	off := k.offset
	for readN < cap(buf) {
		count, err := rr.striper.Read(rr.filename, readP, uint64(off))
		if err != nil {
			return fmt.Errorf("Timeout or Read Error: %s", err.Error())
		}
		if count == 0 {
			break
		}
		readN += count
		off += int64(count)
		readP = buf[count:]
	}
	return dest.SetBytes(buf[:readN])
}
예제 #4
0
func (g widthLimitedImageGetter) Get(ctx groupcache.Context, key string, dest groupcache.Sink) error {
	files, ok := ctx.(map[string]string)
	if !ok {
		return errors.New("Context type wrong. (expecting map[string]string)")
	}
	filePath, ok := files[key]
	if !ok {
		return errors.New(fmt.Sprintf("Requested key (%s) does not exist.", key))
	}
	file, err := os.Open(filePath)
	if err != nil {
		return err
	}
	img, _, err := image.Decode(file)
	file.Close()
	if err != nil {
		return err
	}
	resized := resize.Resize(uint(g), 0, img, resize.NearestNeighbor)
	buf := bytes.NewBuffer(nil)
	err = jpeg.Encode(buf, resized, nil)
	if err != nil {
		return err
	}
	return dest.SetBytes(buf.Bytes())
}
예제 #5
0
파일: gp.go 프로젝트: Wuvist/gws
func getter(ctx groupcache.Context, key string, dest groupcache.Sink) error {
	resp, _ := http.Get(key)
	defer resp.Body.Close()
	body, _ := ioutil.ReadAll(resp.Body)
	log.Printf("asking for %s from solr\n", key)
	dest.SetString(string(body))
	return nil
}
예제 #6
0
func (g fetcherGetter) Get(_ groupcache.Context, key string, dest groupcache.Sink) error {
	q, err := url.ParseQuery(key)
	if err != nil {
		return err
	}
	bytes, err := g.Generate(q)
	if err != nil {
		return err
	}
	return dest.SetBytes(bytes)
}
예제 #7
0
파일: map.go 프로젝트: GeoNet/mtr
func lakeGetter(ctx groupcache.Context, key string, dest groupcache.Sink) error {
	m, err := fromKey(key)
	if err != nil {
		return err
	}

	lakes, err := m.nePolySVG(m.zoom, 1)
	if err != nil {
		return err
	}

	return dest.SetString(lakes)
}
예제 #8
0
func (g zoomedImageGetter) Get(ctx groupcache.Context, key string, dest groupcache.Sink) error {
	bfz := &BuildingFloorZoom{}
	if err := json.Unmarshal([]byte(key), bfz); err != nil {
		return err
	}

	floor := g.s.GetBuildingFloor(bfz.Building, bfz.Floor)
	var err error
	floor.ImageOnce.Do(func() {
		var img, origImg draw.Image
		floor.ImageWG.Add(1)
		defer floor.ImageWG.Done()
		origImg, err = floor.LoadImage()
		if err != nil {
			return
		}

		img = origImg

		if floor.Rotation != 0 {
			rotatedWidth, rotatedHeight := newImageDimentions(origImg, floor.Rotation)
			img = image.NewNRGBA64(image.Rect(0, 0, rotatedWidth, rotatedHeight))
			if err = graphics.Rotate(img, origImg, &graphics.RotateOptions{Angle: floor.Rotation}); err != nil {
				return
			}
		}
		floor.RotatedImage = img
	})
	floor.ImageWG.Wait()
	if err != nil {
		return err
	}

	img := floor.RotatedImage

	coords := ctx.(*models.Coords)
	pixelsPerLongitude := TileSize / coords.DLng()
	pixelsPerLatitude := TileSize / coords.DLat()
	newWidth := floor.Coords.DLng() * pixelsPerLongitude
	newHeight := floor.Coords.DLat() * pixelsPerLatitude

	log.Printf("Generating resized image %f %f", newWidth, newHeight)

	resizedImg := resize.Resize(uint(newWidth), uint(newHeight), img, resize.NearestNeighbor)
	var buf bytes.Buffer
	if err := tileEncoder.Encode(&buf, resizedImg); err != nil {
		return err
	}
	return dest.SetBytes(buf.Bytes())
}
예제 #9
0
func generatorFunc(ctx groupcache.Context, key string, sink groupcache.Sink) error {
	siteURL := ctx.(string)
	icons, err := FetchIcons(siteURL)
	if err != nil {
		// Don't cache errors
		return err
	}

	res := result{Icons: icons}
	if err != nil {
		res.Error = err.Error()
	}
	bytes, err := json.Marshal(res)
	if err != nil {
		panic(err)
	}
	sink.SetBytes(bytes)

	return nil
}
예제 #10
0
파일: map.go 프로젝트: jmptrader/fits
func layerGetter(ctx groupcache.Context, key string, dest groupcache.Sink) error {
	m, err := fromKey(key)
	if err != nil {
		return err
	}

	land, err := m.nePolySVG(m.zoom, 0)
	if err != nil {
		return err
	}

	lakes, err := m.nePolySVG(m.zoom, 1)
	if err != nil {
		return err
	}

	stdDev := 4
	coast := 10

	if m.width > 150 {
		stdDev = 10
		coast = 30
	}

	l, err := m.labels()
	if err != nil {
		return err
	}

	var b bytes.Buffer

	b.WriteString(fmt.Sprintf("<defs><filter id=\"f1\"><feGaussianBlur in=\"SourceGraphic\" stdDeviation=\"%d\" /></filter></defs>", stdDev))
	b.WriteString(fmt.Sprintf("<path stroke-width=\"%d\" stroke-linejoin=\"round\" filter=\"url(#f1)\" stroke=\"azure\" d=\"%s\"/>", coast, land))
	b.WriteString(fmt.Sprintf("<path fill=\"whitesmoke\" stroke-width=\"1\"  stroke-linejoin=\"round\" stroke=\"lightslategrey\" d=\"%s\"/>", land))
	b.WriteString(fmt.Sprintf("<path fill=\"azure\" stroke-width=\"1\"  stroke=\"lightslategrey\" d=\"%s\"/>", lakes))
	b.WriteString(labelsToSVG(l))

	return dest.SetString(b.String())
}