零拷贝读写地理空间数据。
GeoZero定义了一个用于读取地理空间数据格式的API,无需中间表示。它定义了可以实现的特征,用于读取和转换为任意格式或直接渲染几何图形。
支持的几何类型:
支持的维度:X, Y, Z, M, T
| 格式 | 读取 | 写入 | 备注 |
|---|---|---|---|
| GeoJSON | ✅ | ✅ | |
| GEOS | ✅ | ✅ | |
| GDAL | ✅ | ✅ | |
| WKB | ✅ | ✅ | 支持rust-postgres、SQLx和Diesel的PostGIS几何图形。同时也支持SQLx的GeoPackage几何图 形。 |
| WKT | ✅ | ✅ | |
| CSV | ✅ | ✅ | |
| SVG | ❌ | ✅ | |
| geo-types | ✅ | ✅ | |
| MVT(Mapbox矢量瓦片) | ✅ | ✅ | |
| GPX | ✅ | ❌ | |
| Shapefile | ✅ | ❌ | 通过geozero-shpcrate可用。 |
| FlatGeobuf | ✅ | ❌ | 通过flatgeobufcrate可用。 |
| GeoArrow | ✅ | ✅ | 通过geoarrowcrate可用。 |
| GeoParquet | ✅ | ✅ | 通过geoarrowcrate可用。 |
将GeoJSON多边形转换为geo-types并计算质心:
let geojson = GeoJson(r#"{"type": "Polygon", "coordinates": [[[0, 0], [10, 0], [10, 6], [0, 6], [0, 0]]]}"#); if let Ok(Geometry::Polygon(poly)) = geojson.to_geo() { assert_eq!(poly.centroid().unwrap(), Point::new(5.0, 3.0)); }
完整源代码:geo_types.rs
将GeoJSON转换为GEOS预处理几何图形:
let geojson = GeoJson(r#"{"type": "Polygon", "coordinates": [[[0, 0], [10, 0], [10, 6], [0, 6], [0, 0]]]}"#); let geom = geojson.to_geos().expect("GEOS转换失败"); let prepared_geom = geom.to_prepared_geom().expect("to_prepared_geom失败"); let geom2 = geos::Geometry::new_from_wkt("POINT (2.5 2.5)").expect("无效几何图形"); assert_eq!(prepared_geom.contains(&geom2), Ok(true));
完整源代码:geos.rs
将FlatGeobuf子集读取为GeoJSON:
let mut file = BufReader::new(File::open("countries.fgb")?); let mut fgb = FgbReader::open(&mut file)?.select_bbox(8.8, 47.2, 9.5, 55.3)?; println!("{}", fgb.to_json()?);
完整源代码:geojson.rs
将FlatGeobuf数据读取为geo-types几何图形,并使用polylabel-rs计算标签位置:
let mut file = BufReader::new(File::open("countries.fgb")?); let mut fgb = FgbReader::open(&mut file)?.select_all()?; while let Some(feature) = fgb.next()? { let name: String = feature.property("name").unwrap(); if let Ok(Geometry::MultiPolygon(mpoly)) = feature.to_geo() { if let Some(poly) = &mpoly.0.iter().next() { let label_pos = polylabel(&poly, &0.10).unwrap(); println!("{name}: {label_pos:?}"); } } }
完整源代码:polylabel.rs
使用rust-postgres选择和插入geo-types几何图形。需要with-postgis-postgres特性:
let mut client = Client::connect(&std::env::var("DATABASE_URL").unwrap(), NoTls)?; let row = client.query_one( "SELECT 'SRID=4326;POLYGON ((0 0, 2 0, 2 2, 0 2, 0 0))'::geometry", &[], )?; let value: wkb::Decode<geo_types::Geometry<f64>> = row.get(0); if let Some(geo_types::Geometry::Polygon(poly)) = value.geometry { assert_eq!( *poly.exterior(), vec![(0.0, 0.0), (2.0, 0.0), (2.0, 2.0), (0.0, 2.0), (0.0, 0.0)].into() ); } // 插入几何图形 let geom: geo_types::Geometry<f64> = geo::Point::new(1.0, 3.0).into(); let _ = client.execute( "INSERT INTO point2d (datetimefield,geom) VALUES(now(),ST_SetSRID($1,4326))", &[&wkb::Encode(geom)], );
使用SQLx选择和插入geo-types几何图形。需要with-postgis-sqlx特性:
let pool = PgPoolOptions::new() .max_connections(5) .connect(&env::var("DATABASE_URL").unwrap()) .await?;
让 row: (wkb::Decode<geo_types::Geometry<f64>>,) = sqlx::query_as("SELECT 'SRID=4326;POLYGON ((0 0, 2 0, 2 2, 0 2, 0 0))'::geometry") .fetch_one(&pool) .await?; let value = row.0; if let Some(geo_types::Geometry::Polygon(poly)) = value.geometry { assert_eq!( *poly.exterior(), vec![(0.0, 0.0), (2.0, 0.0), (2.0, 2.0), (0.0, 2.0), (0.0, 0.0)].into() ); }
// 插入几何体 let geom: geo_types::Geometry<f64> = geo::Point::new(10.0, 20.0).into(); let _ = sqlx::query( "INSERT INTO point2d (datetimefield,geom) VALUES(now(),ST_SetSRID($1,4326))", ) .bind(wkb::Encode(geom)) .execute(&pool) .await?;
使用编译时验证需要[类型重写](https://docs.rs/sqlx/latest/sqlx/macro.query.html#force-a-differentcustom-type):
```rust,ignore
let _ = sqlx::query!(
"INSERT INTO point2d (datetimefield, geom) VALUES(now(), $1::geometry)",
wkb::Encode(geom) as _
)
.execute(&pool)
.await?;
struct PointRec {
pub geom: wkb::Decode<geo_types::Geometry<f64>>,
pub datetimefield: Option<OffsetDateTime>,
}
let rec = sqlx::query_as!(
PointRec,
r#"SELECT datetimefield, geom as "geom!: _" FROM point2d"#
)
.fetch_one(&pool)
.await?;
assert_eq!(
rec.geom.geometry.unwrap(),
geo::Point::new(10.0, 20.0).into()
);
完整源代码:postgis.rs
计算输入几何体的顶点数:
struct VertexCounter(u64); impl GeomProcessor for VertexCounter { fn xy(&mut self, _x: f64, _y: f64, _idx: usize) -> Result<()> { self.0 += 1; Ok(()) } } let mut vertex_counter = VertexCounter(0); geometry.process(&mut vertex_counter, GeometryType::MultiPolygon)?;
完整源代码:geozero-api.rs
寻找3D多边形中的最大高度:
struct MaxHeightFinder(f64); impl GeomProcessor for MaxHeightFinder { fn coordinate(&mut self, _x: f64, _y: f64, z: Option<f64>, _m: Option<f64>, _t: Option<f64>, _tm: Option<u64>, _idx: usize) -> Result<()> { if let Some(z) = z { if z > self.0 { self.0 = z } } Ok(()) } } let mut max_finder = MaxHeightFinder(0.0); while let Some(feature) = fgb.next()? { let geometry = feature.geometry().unwrap(); geometry.process(&mut max_finder, GeometryType::MultiPolygon)?; }
完整源代码:geozero-api.rs
渲染多边形:
struct PathDrawer<'a> { canvas: &'a mut CanvasRenderingContext2D, path: Path2D, } impl<'a> GeomProcessor for PathDrawer<'a> { fn xy(&mut self, x: f64, y: f64, idx: usize) -> Result<()> { if idx == 0 { self.path.move_to(vec2f(x, y)); } else { self.path.line_to(vec2f(x, y)); } Ok(()) } fn linestring_end(&mut self, _tagged: bool, _idx: usize) -> Result<()> { self.path.close_path(); self.canvas.fill_path( mem::replace(&mut self.path, Path2D::new()), FillRule::Winding, ); Ok(()) } }
完整源代码:flatgeobuf-gpu
使用异步HTTP客户端读取FlatGeobuf数据集,应用边界框过滤器并转换为GeoJSON:
let url = "https://flatgeobuf.org/test/data/countries.fgb"; let mut fgb = HttpFgbReader::open(url) .await? .select_bbox(8.8, 47.2, 9.5, 55.3) .await?; let mut fout = BufWriter::new(File::create("countries.json")?); let mut json = GeoJsonWriter::new(&mut fout); fgb.process_features(&mut json).await?;
完整源代码:geojson.rs
使用kdbush创建KD树索引:
struct PointIndex { pos: usize, index: KDBush, } impl geozero::GeomProcessor for PointIndex { fn xy(&mut self, x: f64, y: f64, _idx: usize) -> Result<()> { self.index.add_point(self.pos, x, y); self.pos += 1; Ok(()) } } let mut points = PointIndex { pos: 0, index: KDBush::new(1249, DEFAULT_NODE_SIZE), }; read_geojson_geom(&mut f, &mut points)?; points.index.build_index();
完整源代码:kdbush.rs


免费创建高清无水印Sora视频
Vora是一个免费创建高清无水印Sora视频的AI工具


最适合小白的AI自动化工作流平台
无需编码,轻松生成可复用、可变现的AI自动化工作流

大模型驱动的Excel数据处理工具
基于大模型交互的表格处理系统,允许用户通过对话方式完成数据整理和可视化分析。系统采用机器学习算法解析用户指令,自动执行排序、公式计算和数据透视等操作,支持多种文件格式导入导出。数据处理响应速度保持在0.8秒以内,支持超过100万行数据的即时分析。


AI辅助编程,代码自动修复
Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。


AI论文写作指导平台
AIWritePaper论文写作是一站式AI论文写作辅助工具,简化了选题、文献检索至论文撰写的整个过程。通过简单设定,平台可快速生成高质量论文大纲和全文,配合图表、参考文献等一应俱全,同时提供开题报告和答辩PPT等增值服务,保障数据安全,有效提升写作效率和论文质量。


AI一键生成PPT,就用博思AIPPT!
博思AIPPT,新一代的AI生成PPT平台,支持智能生成PPT、AI美化PPT、文本&链接生成PPT、导入Word/PDF/Markdown文档生成PPT等,内置海量精美PPT模板,涵盖商务、教育、科技等不同风格,同时针对每个页面提供多种版式,一键自适应切换,完美适配各种办公场景。


AI赋能电商视觉革命,一站式智能商拍平台
潮际好麦深耕服装行业,是国内AI试衣效果最 好的软件。使用先进AIGC能力为电商卖家批量提供优质的、低成本的商拍图。合作品牌有Shein、Lazada、安踏、百丽等65个国内外头部品牌,以及国内10万+淘宝、天猫、京东等主流平台的品牌商家,为卖家节省将近85%的出图成本,提升约3倍出图效率,让品牌能够快速上架。


企业专属的AI法律顾问
iTerms是法大大集团旗下法律子品牌,基于最先进的大语言模型(LLM)、专业的法律知识库和强大的智能体架构,帮助企业扫清合规障碍,筑牢风控防线,成为您企业专属的AI法律顾问。


稳定高效的流量提升解决方案,助力品牌曝光
稳定高效的流量提升解决方案,助力品牌曝光


最新版Sora2模型免费使用,一键生成无水印视频
最新版Sora2模型免费使用,一键生成无水印视频
最新AI工具、AI资讯
独家AI资源、AI项目落地

微信扫一扫关注公众号