def extract_coordinates(linestring, vertex_count): coords_str = linestring.replace("LINESTRING(", "").replace(")", "") coords_list = coords_str.split(", ") coords = [(float(coord.split()[0]), float(coord.split()[1])) for coord in coords_list] return coords[:vertex_count]

时间: 2023-06-12 22:06:32 浏览: 37
这是一个 Python 函数,通过传入一个字符串类型的线串和一个整数类型的顶点数,返回线串中前几个顶点的坐标列表。 具体来说,它首先将线串中的 "LINESTRING(" 和 ")" 字符替换为空字符串,然后用 ", " 将剩余的字符串分割成一个坐标字符串列表。接着,它使用列表推导式将每个坐标字符串转换为一个包含两个浮点数的元组,并将结果存储在一个坐标元组列表中。最后,函数返回前 vertex_count 个坐标元组。 需要注意的是,该函数假定输入的线串是符合标准的 WKT 格式,即以 "LINESTRING(" 开头,以 ")" 结尾,并用 ", " 分隔每个坐标元组。如果输入的线串格式不正确,函数可能会出现异常。
相关问题

def extract_coordinates(linestring, vertex_count): coords_str = linestring.replace("LINESTRING(", "").replace(")", "") coords_list = coords_str.split(", ") coords = [(float(coord.split()[0]), float(coord.split()[1])) for coord in coords_list]

这是一个 Python 函数,用于从一个字符串表示的线段中提取顶点坐标。具体来说,它需要两个参数: - `linestring`:一个字符串,表示一个线段。这个字符串的格式应该为 "LINESTRING(x1 y1, x2 y2, ..., xn yn)",其中 xi 和 yi 是浮点数,表示第 i 个顶点的坐标。 - `vertex_count`:一个整数,表示要提取的顶点数量。 函数会首先从 `linestring` 中提取出坐标字符串,然后将其分割成一个列表。接着,函数会遍历这个列表,将每个坐标字符串解析成一个二元组,表示该顶点的坐标。最后,函数会返回一个包含前 `vertex_count` 个顶点坐标的列表。 注意,这个函数假设输入的线段是符合格式要求的,如果输入的格式不正确,函数可能会抛出异常。

class Point: def __init__(self, x, y): self.__x = x self.__y = y def __add__(self, other): x = self.__x + other.__x y = self.__y + other.__y return Point(x, y) def __sub__(self, other): x = self.__x - other.__x y = self.__y - other.__y return Point(x, y) def __mul__(self, a): x = self.__x * a y = self.__y * a return Point(x, y) def __truediv__(self, a): x = self.__x / a y = self.__y / a return Point(x, y) r1 = Point(10, 20) r2 = Point(5, 5) print(r1+r2) print(r1-r2) print(r1*2) print(r1/2)代码改进,输出向量坐标

为了输出向量坐标,我们可以在 Point 类中添加一个方法来返回坐标的元组,如下所示: ```python class Point: def __init__(self, x, y): self.__x = x self.__y = y def __add__(self, other): x = self.__x + other.__x y = self.__y + other.__y return Point(x, y) def __sub__(self, other): x = self.__x - other.__x y = self.__y - other.__y return Point(x, y) def __mul__(self, a): x = self.__x * a y = self.__y * a return Point(x, y) def __truediv__(self, a): x = self.__x / a y = self.__y / a return Point(x, y) def get_coordinates(self): return (self.__x, self.__y) ``` 然后我们就可以使用这个方法来输出向量坐标了,示例如下: ```python r1 = Point(10, 20) r2 = Point(5, 5) print(r1+r2.get_coordinates()) # 输出 (15, 25) print(r1-r2.get_coordinates()) # 输出 (5, 15) print(r1*2).get_coordinates()) # 输出 (20, 40) print(r1/2).get_coordinates()) # 输出 (5.0, 10.0) ```

相关推荐

我想在以下这段代码中,添加显示标有特征点的图像的功能。def cnn_feature_extract(image,scales=[.25, 0.50, 1.0], nfeatures = 1000): if len(image.shape) == 2: image = image[:, :, np.newaxis] image = np.repeat(image, 3, -1) # TODO: switch to PIL.Image due to deprecation of scipy.misc.imresize. resized_image = image if max(resized_image.shape) > max_edge: resized_image = scipy.misc.imresize( resized_image, max_edge / max(resized_image.shape) ).astype('float') if sum(resized_image.shape[: 2]) > max_sum_edges: resized_image = scipy.misc.imresize( resized_image, max_sum_edges / sum(resized_image.shape[: 2]) ).astype('float') fact_i = image.shape[0] / resized_image.shape[0] fact_j = image.shape[1] / resized_image.shape[1] input_image = preprocess_image( resized_image, preprocessing="torch" ) with torch.no_grad(): if multiscale: keypoints, scores, descriptors = process_multiscale( torch.tensor( input_image[np.newaxis, :, :, :].astype(np.float32), device=device ), model, scales ) else: keypoints, scores, descriptors = process_multiscale( torch.tensor( input_image[np.newaxis, :, :, :].astype(np.float32), device=device ), model, scales ) # Input image coordinates keypoints[:, 0] *= fact_i keypoints[:, 1] *= fact_j # i, j -> u, v keypoints = keypoints[:, [1, 0, 2]] if nfeatures != -1: #根据scores排序 scores2 = np.array([scores]).T res = np.hstack((scores2, keypoints)) res = res[np.lexsort(-res[:, ::-1].T)] res = np.hstack((res, descriptors)) #取前几个 scores = res[0:nfeatures, 0].copy() keypoints = res[0:nfeatures, 1:4].copy() descriptors = res[0:nfeatures, 4:].copy() del res return keypoints, scores, descriptors

class SpiralIterator: def init(self, source, x=810, y=500, length=None): self.source = source self.row = np.shape(self.source)[0]#第一个元素是行数 self.col = np.shape(self.source)[1]#第二个元素是列数 if length: self.length = min(length, np.size(self.source)) else: self.length = np.size(self.source) if x: self.x = x else: self.x = self.row // 2 if y: self.y = y else: self.y = self.col // 2 self.i = self.x self.j = self.y self.iteSize = 0 geo_transform = dsm_data.GetGeoTransform() self.x_origin = geo_transform[0] self.y_origin = geo_transform[3] self.pixel_width = geo_transform[1] self.pixel_height = geo_transform[5] def hasNext(self): return self.iteSize < self.length # 不能取更多值了 def get(self): if self.hasNext(): # 还能再取一个值 # 先记录当前坐标的值 —— 准备返回 i = self.i j = self.j val = self.source[i][j] # 计算下一个值的坐标 relI = self.i - self.x # 相对坐标 relJ = self.j - self.y # 相对坐标 if relJ > 0 and abs(relI) < relJ: self.i -= 1 # 上 elif relI < 0 and relJ > relI: self.j -= 1 # 左 elif relJ < 0 and abs(relJ) > relI: self.i += 1 # 下 elif relI >= 0 and relI >= relJ: self.j += 1 # 右 #判断索引是否在矩阵内 x = self.x_origin + (j + 0.5) * self.pixel_width y = self.y_origin + (i + 0.5) * self.pixel_height z = val self.iteSize += 1 return x, y, z dsm_path = 'C:\sanwei\jianmo\Productions\Production_2\Production_2_DSM_part_2_2.tif' dsm_data = gdal.Open(dsm_path) dsm_array = dsm_data.ReadAsArray() spiral_iterator = SpiralIterator(dsm_array,x=810,y=500) while spiral_iterator.hasNext(): x, y, z = spiral_iterator.get() print(f'Value at ({x},{y}):{z}')这段代码怎么改可以将地面点坐标反算其原始航片对应的像素行列号

最新推荐

recommend-type

麦肯锡-年月―中国xx集团战略咨询项目建议书.ppt

麦肯锡-年月―中国xx集团战略咨询项目建议书.ppt
recommend-type

廖倩5.14运营款.xlsx

廖倩5.14运营款.xlsx
recommend-type

setuptools-25.0.2-py2.py3-none-any.whl

文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

全网第一个宝宝生活记录网站root ca

全网第一个宝宝生活记录网站,帮我写一个网站介绍,网址 https://43.136.21.175/ ,二维码分享和登录,统计记录最近时间,功能很全很实用,手机和电脑都可以访问,手机浏览器可以生成快捷方式到桌面和App一样,点右上角可以打开菜单,里面我做了图表统计可以看每天的喂养次数哪些,我们一直在用,很方便的
recommend-type

yes I can.mp3

yes I can.mp3
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。