geojson转wkt
时间: 2023-11-13 15:05:24 浏览: 38
你可以使用 Python 中的 `pyproj` 库来进行转换。以下是一个示例代码:
```python
import json
from shapely.geometry import shape
from shapely.wkt import dumps
from pyproj import Proj, transform
# 读取GeoJSON文件
with open('input.geojson') as f:
geojson = json.load(f)
# 获取GeoJSON中的几何对象
geom = shape(geojson['features'][0]['geometry'])
# 投影转换
in_proj = Proj(geojson['crs']['properties']['name'])
out_proj = Proj(init='epsg:4326')
geom = transform(in_proj, out_proj, geom)
# 转换为WKT格式
wkt = dumps(geom)
# 输出WKT字符串
print(wkt)
```
你需要将 `input.geojson` 替换为你要转换的文件名,需要确保该文件中只有一个几何对象。此代码将使用 `pyproj` 将输入的GeoJSON文件从其指定的投影转换为WGS84,并将几何对象转换为WKT格式。
相关问题
Geojson转WKT
GeoJSON 是一种开放的数据交换格式,用于表示地理信息。WKT (Well-Known Text) 是另一种用于表示地理信息的格式。
如果你想将 GeoJSON 转换为 WKT,你可以使用各种工具来完成这个转换,例如 ogr2ogr、GDAL、QGIS 等。
例如,你可以使用 ogr2ogr 命令行工具来完成转换。命令的格式如下:
```
ogr2ogr -f "WKT" output.wkt input.geojson
```
其中,`input.geojson` 是你想要转换的 GeoJSON 文件的路径,`output.wkt` 是转换后输出的 WKT 文件的路径。
python中geojson转wkt的代码
可以使用 `shapely` 库来将 `GeoJSON` 格式的几何对象转换为 `WKT` 格式的字符串。以下是一个示例代码:
```python
from shapely.geometry import shape
# GeoJSON格式的几何对象
geojson = {
"type": "Point",
"coordinates": [
-122.419416,
37.774929
]
}
# 将GeoJSON格式的几何对象转换为Shapely的几何对象
geom = shape(geojson)
# 将Shapely的几何对象转换为WKT格式的字符串
wkt = geom.wkt
print(wkt)
# 输出: POINT (-122.419416 37.774929)
```
注意:如果你的 `GeoJSON` 中有多个几何对象,则需要循环遍历每个几何对象并将它们分别转换为 `WKT` 格式的字符串。