unsupported operand type(s) for %: 'CellId' and 'int'
时间: 2023-08-02 13:08:33 浏览: 56
抱歉,之前的代码中出现了错误。在记录 `CELL_ID` 属性时,`CellId` 对象不能直接转换为字符串。您可以使用 `id().id()` 方法获取单元格的 ID,并将其转换为字符串类型。
以下是修正后的示例代码:
```python
import shapefile
from s2sphere import CellId, LatLngRect, LatLng, S2RegionCoverer
min_lat = 40.0 # 最小纬度
max_lat = 41.0 # 最大纬度
min_lng = -74.0 # 最小经度
max_lng = -73.0 # 最大经度
shp_writer = shapefile.Writer("cells", shapeType=shapefile.POLYGON)
shp_writer.field("CELL_ID", "C", size=50)
rect = LatLngRect.from_point_pair(
LatLng.from_degrees(min_lat, min_lng),
LatLng.from_degrees(max_lat, max_lng)
)
coverer = S2RegionCoverer()
covering = coverer.get_covering(rect)
for cell_id in covering:
cell = CellId(cell_id).to_lat_lng_rect()
vertices = [
(cell.lo().lat().degrees, cell.lo().lng().degrees),
(cell.lo().lat().degrees, cell.hi().lng().degrees),
(cell.hi().lat().degrees, cell.hi().lng().degrees),
(cell.hi().lat().degrees, cell.lo().lng().degrees),
]
shp_writer.poly(parts=[vertices])
shp_writer.record(str(cell_id.id()))
shp_writer.save("cells")
```
现在,使用 `cell_id.id()` 方法将 `CellId` 对象转换为整数 ID,并将其转换为字符串类型以进行记录。
再次抱歉造成的困扰,并希望以上修复能够解决问题。如果您还有其他问题,请随时提问!