mirror of https://github.com/tidwall/tile38.git
removed gzipped feature
This commit is contained in:
parent
63268dad3f
commit
d47279e1ff
|
@ -2,9 +2,7 @@ package geojson
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"compress/gzip"
|
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"io/ioutil"
|
|
||||||
|
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
"github.com/tidwall/tile38/geojson/geohash"
|
"github.com/tidwall/tile38/geojson/geohash"
|
||||||
|
@ -14,7 +12,7 @@ import (
|
||||||
type Feature struct {
|
type Feature struct {
|
||||||
Geometry Object
|
Geometry Object
|
||||||
BBox *BBox
|
BBox *BBox
|
||||||
idprops []byte // raw id and properties combined
|
idprops string // raw id and properties seperated by a '\0'
|
||||||
}
|
}
|
||||||
|
|
||||||
func fillFeatureMap(json string) (Feature, error) {
|
func fillFeatureMap(json string) (Feature, error) {
|
||||||
|
@ -94,30 +92,25 @@ func (g Feature) MarshalJSON() ([]byte, error) {
|
||||||
return []byte(g.JSON()), nil
|
return []byte(g.JSON()), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g Feature) getRaw() (id, props []byte) {
|
func (g Feature) getRaw() (id, props string) {
|
||||||
if len(g.idprops) == 0 {
|
if len(g.idprops) == 0 {
|
||||||
return nil, nil
|
return "", ""
|
||||||
}
|
}
|
||||||
buf := g.idprops
|
switch g.idprops[0] {
|
||||||
rd, err := gzip.NewReader(bytes.NewReader(buf))
|
|
||||||
if err == nil {
|
|
||||||
buf, _ = ioutil.ReadAll(rd)
|
|
||||||
}
|
|
||||||
switch buf[0] {
|
|
||||||
default:
|
default:
|
||||||
lnp := int(buf[0]) + 1
|
lnp := int(g.idprops[0]) + 1
|
||||||
return buf[1:lnp], buf[lnp:]
|
return g.idprops[1:lnp], g.idprops[lnp:]
|
||||||
case 255:
|
case 255:
|
||||||
lnp := int(binary.LittleEndian.Uint64([]byte(buf[1:9]))) + 9
|
lnp := int(binary.LittleEndian.Uint64([]byte(g.idprops[1:9]))) + 9
|
||||||
return buf[9:lnp], buf[lnp:]
|
return g.idprops[9:lnp], g.idprops[lnp:]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeCompositeRaw(idRaw, propsRaw string) []byte {
|
func makeCompositeRaw(idRaw, propsRaw string) string {
|
||||||
idRaw = stripWhitespace(idRaw)
|
idRaw = stripWhitespace(idRaw)
|
||||||
propsRaw = stripWhitespace(propsRaw)
|
propsRaw = stripWhitespace(propsRaw)
|
||||||
if len(idRaw) == 0 && len(propsRaw) == 0 {
|
if len(idRaw) == 0 && len(propsRaw) == 0 {
|
||||||
return nil
|
return ""
|
||||||
}
|
}
|
||||||
var raw []byte
|
var raw []byte
|
||||||
if len(idRaw) > 0xFF-1 {
|
if len(idRaw) > 0xFF-1 {
|
||||||
|
@ -132,16 +125,7 @@ func makeCompositeRaw(idRaw, propsRaw string) []byte {
|
||||||
copy(raw[1:], idRaw)
|
copy(raw[1:], idRaw)
|
||||||
copy(raw[len(idRaw)+1:], propsRaw)
|
copy(raw[len(idRaw)+1:], propsRaw)
|
||||||
}
|
}
|
||||||
if len(raw) < 256 {
|
return string(raw)
|
||||||
return raw
|
|
||||||
}
|
|
||||||
var buf bytes.Buffer
|
|
||||||
gbuf := gzip.NewWriter(&buf)
|
|
||||||
gbuf.Write(raw)
|
|
||||||
gbuf.Close()
|
|
||||||
tight := make([]byte, len(buf.Bytes()))
|
|
||||||
copy(tight, buf.Bytes())
|
|
||||||
return tight
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// JSON is the json representation of the object. This might not be exactly the same as the original.
|
// JSON is the json representation of the object. This might not be exactly the same as the original.
|
||||||
|
@ -151,13 +135,13 @@ func (g Feature) JSON() string {
|
||||||
buf.WriteString(g.Geometry.JSON())
|
buf.WriteString(g.Geometry.JSON())
|
||||||
g.BBox.write(&buf)
|
g.BBox.write(&buf)
|
||||||
idRaw, propsRaw := g.getRaw()
|
idRaw, propsRaw := g.getRaw()
|
||||||
if len(propsRaw) != 0 {
|
if propsRaw != "" {
|
||||||
buf.WriteString(`,"properties":`)
|
buf.WriteString(`,"properties":`)
|
||||||
buf.Write(propsRaw)
|
buf.WriteString(propsRaw)
|
||||||
}
|
}
|
||||||
if len(idRaw) != 0 {
|
if idRaw != "" {
|
||||||
buf.WriteString(`,"id":`)
|
buf.WriteString(`,"id":`)
|
||||||
buf.Write(idRaw)
|
buf.WriteString(idRaw)
|
||||||
}
|
}
|
||||||
buf.WriteByte('}')
|
buf.WriteByte('}')
|
||||||
return buf.String()
|
return buf.String()
|
||||||
|
|
|
@ -1,9 +1,6 @@
|
||||||
package geojson
|
package geojson
|
||||||
|
|
||||||
import (
|
import "testing"
|
||||||
"fmt"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestFeature(t *testing.T) {
|
func TestFeature(t *testing.T) {
|
||||||
testJSON(t, `{
|
testJSON(t, `{
|
||||||
|
@ -117,7 +114,5 @@ func TestComplexFeature(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
return
|
o = o
|
||||||
println(len(o.(Feature).idprops), cap(o.(Feature).idprops))
|
|
||||||
fmt.Printf("%v\n", o.JSON())
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue