地图框矢量平铺

mapbox-vector-tile的Python项目详细描述


地图框矢量图块[构建状态](https://travis-ci.org/tilezen/mapbox-vector-tile.svg?branch=master)(https://travis ci.org/tilezen/mapbox vector tile)
[![覆盖状态](https://coveralls.io/repos/github/tilezen/mapbox-vector-tile/badge.svg?branch=master)](https://coveralls.io/github/tilezen/mapbox-vector-tile?branch=master)

它在pypi上被列为"mapbox vector tile"。建议通过"pip"安装:

`` shell
pip install mapbox vector tile
```

注意,"mapbox vector tile"依赖于[shapely](https://pypi.python.org/pypi/shapely),一个用于计算几何的python库,它需要一个名为[geos]的库(https://trac.osgeo.org/geos/)。有关如何安装其先决条件的信息,请参阅[Shapely的说明](https://pypi.python.org/pypi/shapely installing shapely)。


encoding
--


encode方法需要一个层数组或至少一个有效层。有效层是具有以下键的字典

*`name`:layer name
*`features`:features数组。特征是具有以下键的字典:

*`geometry`:特征几何图形在wkt、wkb或shapely几何图形中的表示。坐标是相对于平铺的,缩放范围为`[04096`'。请参见下面的示例代码以执行必要的转换。*注意*不支持"geometrycollection"类型,并将触发"valueerror"。
*"properties":具有几个键及其对应值的字典。

``python

>;>;使用wkt导入mapbox矢量图块

>;>;mapbox_vector_tile.encode([
{
"name":"water",
"features":[
{
"geometry":"polygon((0,0 1,1 1 1,1 0,0 0))",
"properties":{
"uid":123,
"foo":"bar",
"cat":"flied"
}
}
]
},
{
"name":"air",
"features":[
{
"geometry":"linestring(159 3877,-1570 3877)",
"properties":{
"uid":1234,
"foo":"酒吧","猫":"飞"了
















]

<
>'\x1ah\n\x05水\x12\x18\x12\x18\x12\x06\x00\x00\x00\x01\x01\x02\x02\x18\x03"\x0c\t\x0c\t\x00\x00\x80\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03CAT"\x05\n\x03bar"\x02{"\x06\x06\n\x04飞(\x80 x\x02\x02\x1ad\n\x03Air\x12\x15\x12\x12\x06\x00\x00\x01\x01\x02\x02\x18\x02"\t\t\xbe\x02\x06\x06\x00\x01\x01\x01\x02\x02\x02\x06\x06\x04飞(\x80 x\x06\x02\x00\x01\x01\x02\x02\x02\x02\x02\x02\x02\x118\x18\x02\x02"\t\t\t\t\t\t\t\x02"\t\t\t\t 02'



mapbox_vector_tile.encode([
{
"name":"water",
"features":[
{
"geometry":"\001\003\000\000\000\001\000\000\000\005\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\360?\ 000\000\000\000\000\000\360?\ 000\000\000\000\000\000\360?\ 000\000\000\000\000\000\360?\ 000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000",
"properties":{
"uid":123,
"foo":"bar",
"cat":"flied"
}
]
]
},
{
"name":"air",
"features":[
{
"geometry":"\001\003\000\000\000\001\000\000\000\005\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\ 000\000\000\000\000\000\360?\ 000\000\000\000\000\000\360?\ 000\000\000\000\000\000?360?\ 000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000",
"properties":{
"uid":1234,
"foo":"bar","猫":"飞了"















\x1aj\n\x05水\x12\x1a\x08\x01\x01\x00\x00\x01\x01\x01\x02\x02\x02\x118\x03"\x0c\t\x00\x00\x00\x00\x02\x02\x02\x02\x0f\x1a\x03foo\x03foo\x1a\x03foo\x1a\x01\x01\x01\x01\x01\03uid\x1a\x03 cat"\x05\n\x03bar"\x02{"\x06\n\x04flew(\x80 x\x02\x1aY\n\x03air\x12\x1c\x08\x01\x12\x08\x00\x00\x01\x01\x02\x02\x03\x03\x18\x03"\x0c\t\x00\x80@\x1a\x00\x01\x02\x00\x00\x02\x0f\x1a\x03foo\x1a\x03uid\x1a\x05balls\x1a\x03cat"\x05\n\x03bar"\x03 \xd2\t"\x05\n\x03foo"\x06\n\x04flew(\x80 x\x02'
```

### Coordinate 编码转换

编码器需要几何图形:

1。在平铺相对坐标系中,左下角为原点,值向上和向右增长,平铺为4096像素正方形。例如,`point(0 0)`是平铺的左下角,`point(4096,4096)`是平铺的右上角。在这种情况下,库不进行投影,坐标按原样编码。
2。在另一个坐标系中,由"quantize\u bounds"参数给出平铺边界。在这种情况下,库将缩放坐标,以便"量化界限"在"x"和"y"方向的范围(0,4096)内。除了仿射变换之外,库没有其他投影。

默认值为"y up"。

通过将调用的"extents"参数设置为"encode()",可以控制平铺范围(默认值为4096,如以上示例中所用)。默认值为4096。

此示例代码使用django包含的geos库对"linestring"对象进行转换:

``python
`srid_spheral_mercator=3857


`def linestring_in_tile(tile_bounds,line):
`mapbox vector tile`具有4096个单位的硬编码tile范围。
mvt_extent=4096
来自django.contrib.gis.geos import linestring

assert line.srid不是none
line.transform(srid_spheral_mercator)

(x0,y0,x_max,y_max)=平铺边界。extent
x_span=x_max-x0
y_span=y_max-y0
def xy_pairs():
对于x_merc,y_merc in line:
屈服(
int((x_merc-x0)*mvt_extent/x_span)、
int((y_merc-y0)*mvt_extent/y_span)、
````

一个完整的用法示例可能是这样的:

`` python
来自django.contrib.gis.geos import linestring,polygon
import mercantile
import mapbox_vector_tile

srid_spheral_mercator=3857

tile_xyz=(2452,3422,18)
tile_bounds=polygon.from_bbox(mercantile.bounds(*tile_xyz))
tile_bounds.srid=srid_lnglat
tile_bounds.transform(srid_spheral_mercator)

lnglat_line=linestring(((-122.1,45.1),(-122.2,45.2)),srid=srid_lnglat)
tile_line=linestring_in_tile(tile_bounds,tile_bounds,tile_)lnglat_line)
tile_pbf=mapbbox_vector_tile.encode({
"name":"my layer",
"features":[{
"geometry":tile_line.wkt,
"properties":{"stuff":"things"},
}]
}
````

请注意,在呈现此示例时,该平铺中可能没有任何可见的内容。这取决于你是否把正确的数据放在磁贴上!

还需要注意的是,规范允许修改扩展数据块,即使它们通常按照惯例设置为4096。` mapbox vector tile`假定范围为4096。


编码时,以形式(minx,miny,maxx,maxy)传入边界,编码时坐标将适当缩放。

``python
mapbox\u vector\u tile.encode([
{
"name":"water",
"features":[
{
"geometry":"point(15 15)",
"properties":{
"foo":"bar",
}
}
]
}
}
}
],量化界限=(10.0,10.0,20.0,20.0))
````

将被编码的坐标是(20482048)

这将禁止在编码过程中翻转Y坐标值。


这些将传递到PBF中的层,并在任何量化或Y坐标翻转期间执行。

``python
mapbox_vector_tile.encode([
{
"name":"water",
"features":[
{
"geometry":"point(15 15)",
"properties":{
"foo":"bar",
}
}
]
}
}
}
],量化范围=(0.0,0.0,10.0,10.0),范围=50)
`````


decimal模块用于解释性地将round-half-even定义为舍入方法。这可能比内置的"round()"函数慢。encode接受一个可选的"round fn",您可以在其中指定要使用的round函数。

``python
mapbox\u vector\u tile.encode([
{
"name":"water",
"features":[
{
"geometry":"point(15 15)",
"properties":{
"foo":"bar",
}
}
]
}
}
],量化界限=(0.0,0.0,10.0,10.0),round-fn=round)
```

decoding
--


decode方法接受有效的google.protobuf.message平铺并以以下格式返回解码字符串:

``python
{
layername:{
'extent':'integer layer extent'
'version':"integer"
"features":[{
"geometry":"list of points",
"properties":"dictionary of key/value pairs",
"id":"层内给定功能的唯一id"
},…
]
},
图层名称2:{

}
}
```````
``python
>;>import mapbox\u vector\u tile

>;>地图箱就是矢量图。解码('\x1aj\n\x05水\x12\x1a\x08\x08\x01\x12\x06\x06\x00\x00\x01\x01\x02\x02\x02\x18\x03"\x0c\t\x00\x00\x80@\x1a\x00\x01\x01\x01\x02\x00\x00\x02\x02\x0f\x1a\x03foo\x1a\x03uid\x1a\x03cat"\x05\x05\n\x03bar"\x02\x02{"\x06\x06\x06\x06\n\x00\x00\x00\x00\x00\x01\xx04飞行(\x80x\x02\x1aY\n\x03air\x12\x1c\x08\x01\x12\x08\x00\x00\x01\x01\x02\x02\x03\x03\x18\x03"\x0c\t\x00\x80@\x1a\x00\x01\x02\x00\x00\x02\x0f\x1a\x03foo\x1a\x03uid\x1a\x05balls\x1a\x03cat"\x05\n\x03bar"\x03 \xd2\t"\x05\n\x03foo"\x06\n\x04flew(\x80 x\x02')

{
'water': {
'extent': 4096,
"版本":2,
"功能":[{
"几何体":{"类型":"多边形","坐标":[[0,0],[0,1],[1,1],[1,0],[0,0]},
"属性":{
"foo":'bar',
'uid':123,
'cat':'flied'
},
'type':3,
'id':1
}
]
},
'air':{
'extent':4096,
'version':2,
"功能":[{
"几何体":{"类型":"多边形","坐标":[[0,0],[0,1],[1,1],[1,0],[0,0]]},
"属性":{
"foo":"bar","uid":1234,
"球":"foo","cat":"flied"

"type":3,
"id":1
}
]
}
}
}
````

>下面是如何从文件中解码磁贴的方法。

``python
>;
>;导入mapbox矢量磁贴
>;,将open('tile.mvt','rb')作为f:
>;>data=f.read()
>;>decoded_data=mapbox_vector_tile.decode(data)
>;>F.Script(RePR(译码数据))
'BR/> BR/>使用本地原BUF库进行性能BR/> ------------------------------BR/> BR/>下面的原BUBF库的C++实现比纯Python库更具性能。根据你的操作系统,您可能需要[编译C++库](http://Github.com/谷歌/OrthBuff/Trime/Myth/Python C实现)或安装它。
BR/>在Debian Jesie
> Br/> Debian杰西上的原始BuffFF(LIbToBuffF9)版本[2.6.1](https://github.com/google/protobuf/tree/v2.6.1/python)。您可以从包管理器中使用正确的python绑定安装它:

$sudo apt get install libprotoc9 libprotobuf9 protobuf compiler python protobuf

>然后,在运行python程序之前,必须启用两个环境变量:

$exportprotocol_buffers_python_implementation=cpp
$export protocol_buffers_python_implementation=2



--


单击[此处](https://github.com/tilezen/mapbox-vector-tile/blob/master/changelog.md)查看不同版本中随时间变化的内容。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java中char的用途   swing修复,以便显示可以同时显示十进制数和整数   java给定一个场景,找到拥有的JFXPanel   java JPA 2标准获取路径导航   java如何用JUnit测试这个类?   JSF中的java请求作用域   Spring云Zuul代理背后的java Spring OAuth授权服务器   java限制聚合返回的存储桶大小   java如何在安卓应用程序中请求超级su权限   防止超级关键字的java Checkstyle规则   java 安卓。应用程序。应用程序无法强制转换到安卓。应用程序。在回收器适配器中使用FragmentManager时的活动   使用Java的Twitter广告API   切换到4.0.1版本后,java“JavaMailSenderImpl无法解析为类型”   java没有getter方法可用于名称下bean的属性   java 0/1背包堆空间   java我无法使用断点进行调试   Jar文件名表单java代码   java调用从扩展活动的MainActivity类扩展片段的类片段