{"id":1024535,"date":"2024-12-30T14:14:43","date_gmt":"2024-12-30T06:14:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1024535.html"},"modified":"2024-12-30T14:14:44","modified_gmt":"2024-12-30T06:14:44","slug":"%e7%94%a8python%e5%a6%82%e4%bd%95%e6%8f%90%e5%8f%96%e5%9d%90%e6%a0%87-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1024535.html","title":{"rendered":"\u7528python\u5982\u4f55\u63d0\u53d6\u5750\u6807"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/cf65f81f-75eb-4d8c-a314-869d5cdfaf40.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u7528python\u5982\u4f55\u63d0\u53d6\u5750\u6807\" \/><\/p>\n<p><p> \u5f00\u5934\u6bb5\u843d: \u4f7f\u7528Python\u63d0\u53d6\u5750\u6807\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528<strong>\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u7b2c\u4e09\u65b9\u5730\u7406\u4fe1\u606f\u5e93\u3001\u56fe\u50cf\u5904\u7406\u5e93<\/strong>\u7b49\u65b9\u5f0f\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u9002\u5408\u4ece\u6587\u672c\u4e2d\u63d0\u53d6\u5750\u6807\uff0c\u7b2c\u4e09\u65b9\u5730\u7406\u4fe1\u606f\u5e93\u5982geopy\u3001geopython\u7b49\u9002\u5408\u5904\u7406\u5730\u7406\u5750\u6807\u6570\u636e\uff0c\u56fe\u50cf\u5904\u7406\u5e93\u5982OpenCV\u9002\u5408\u4ece\u56fe\u50cf\u4e2d\u63d0\u53d6\u5750\u6807\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u63d0\u53d6\u5750\u6807\uff0c\u5e76\u7ed3\u5408\u5177\u4f53\u5b9e\u4f8b\u8fdb\u884c\u8bf4\u660e\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u5750\u6807<\/p>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6587\u672c\u641c\u7d22\u548c\u5904\u7406\u5de5\u5177\uff0c\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u4ece\u6587\u672c\u4e2d\u63d0\u53d6\u51fa\u7b26\u5408\u7279\u5b9a\u6a21\u5f0f\u7684\u5b57\u7b26\u4e32\u3002\u5bf9\u4e8e\u5750\u6807\u6570\u636e\uff0c\u6211\u4eec\u901a\u5e38\u91c7\u7528\u7ecf\u7eac\u5ea6\u8868\u793a\u6cd5\uff0c\u5982\u201c(34.052235, -118.243683)\u201d\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u5750\u6807\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5b9a\u4e49\u5750\u6807\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u3002<\/li>\n<li>\u4f7f\u7528re\u5e93\u5bf9\u6587\u672c\u8fdb\u884c\u5339\u914d\u3002<\/li>\n<li>\u63d0\u53d6\u5339\u914d\u7684\u7ed3\u679c\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def extract_coordinates(text):<\/p>\n<p>    # \u5b9a\u4e49\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f<\/p>\n<p>    pattern = r&quot;\\(\\s*([-+]?\\d*\\.?\\d+)\\s*,\\s*([-+]?\\d*\\.?\\d+)\\s*\\)&quot;<\/p>\n<p>    # \u4f7f\u7528re\u5e93\u8fdb\u884c\u5339\u914d<\/p>\n<p>    matches = re.findall(pattern, text)<\/p>\n<p>    # \u63d0\u53d6\u5339\u914d\u7ed3\u679c<\/p>\n<p>    coordinates = [(float(match[0]), float(match[1])) for match in matches]<\/p>\n<p>    return coordinates<\/p>\n<p>text = &quot;Here are some coordinates: (34.052235, -118.243683), (40.712776, -74.005974).&quot;<\/p>\n<p>print(extract_coordinates(text))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>[(34.052235, -118.243683), (40.712776, -74.005974)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5730\u7406\u4fe1\u606f\u5e93\u63d0\u53d6\u5750\u6807<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5904\u7406\u5730\u7406\u4fe1\u606f\u6570\u636e\uff0cPython\u6709\u8bb8\u591a\u5f3a\u5927\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982geopy\u3001geopython\u7b49\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u5730\u7406\u5750\u6807\u7684\u63d0\u53d6\u3001\u8f6c\u6362\u548c\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<ol>\n<li>geopy\u5e93\u7684\u4f7f\u7528<\/li>\n<\/ol>\n<p><p>geopy\u662f\u4e00\u4e2a\u7528\u4e8e\u5904\u7406\u5730\u7406\u4fe1\u606f\u7684Python\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u5730\u5740\u83b7\u53d6\u5750\u6807\uff0c\u901a\u8fc7\u5750\u6807\u83b7\u53d6\u5730\u5740\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from geopy.geocoders import Nominatim<\/p>\n<p>def get_coordinates(address):<\/p>\n<p>    geolocator = Nominatim(user_agent=&quot;geoapiExercises&quot;)<\/p>\n<p>    location = geolocator.geocode(address)<\/p>\n<p>    return (location.latitude, location.longitude)<\/p>\n<p>address = &quot;1600 Amphitheatre Parkway, Mount<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n View, CA&quot;<\/p>\n<p>print(get_coordinates(address))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>(37.4220656, -122.0840897)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>geopython\u5e93\u7684\u4f7f\u7528<\/li>\n<\/ol>\n<p><p>geopython\u662f\u53e6\u4e00\u4e2a\u7528\u4e8e\u5904\u7406\u5730\u7406\u4fe1\u606f\u6570\u636e\u7684Python\u5e93\uff0c\u529f\u80fd\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from geopython import GeoPy<\/p>\n<p>def get_coordinates(address):<\/p>\n<p>    geopy = GeoPy()<\/p>\n<p>    location = geopy.geocode(address)<\/p>\n<p>    return (location.latitude, location.longitude)<\/p>\n<p>address = &quot;1600 Amphitheatre Parkway, Mountain View, CA&quot;<\/p>\n<p>print(get_coordinates(address))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u56fe\u50cf\u5904\u7406\u5e93\u63d0\u53d6\u5750\u6807<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4ece\u56fe\u50cf\u4e2d\u63d0\u53d6\u5750\u6807\uff0cOpenCV\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\u3002\u5b83\u53ef\u4ee5\u8fdb\u884c\u5404\u79cd\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\uff0c\u5305\u62ec\u5750\u6807\u63d0\u53d6\u3002<\/p>\n<\/p>\n<ol>\n<li>\u57fa\u672c\u7684OpenCV\u64cd\u4f5c<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>def extract_coordinates(image_path):<\/p>\n<p>    # \u8bfb\u53d6\u56fe\u50cf<\/p>\n<p>    image = cv2.imread(image_path)<\/p>\n<p>    # \u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/p>\n<p>    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)<\/p>\n<p>    # \u4f7f\u7528\u8fb9\u7f18\u68c0\u6d4b\u63d0\u53d6\u5750\u6807<\/p>\n<p>    edges = cv2.Canny(gray, 50, 150)<\/p>\n<p>    # \u5bfb\u627e\u8f6e\u5ed3<\/p>\n<p>    contours, _ = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)<\/p>\n<p>    # \u63d0\u53d6\u5750\u6807<\/p>\n<p>    coordinates = [cv2.boundingRect(contour) for contour in contours]<\/p>\n<p>    return coordinates<\/p>\n<p>image_path = &quot;path\/to\/your\/image.jpg&quot;<\/p>\n<p>print(extract_coordinates(image_path))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u8f93\u51fa\u56fe\u50cf\u4e2d\u6240\u6709\u8f6e\u5ed3\u7684\u5750\u6807\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u9ad8\u7ea7OpenCV\u64cd\u4f5c<\/li>\n<\/ol>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u9700\u6c42\uff0c\u53ef\u4ee5\u4f7f\u7528OpenCV\u7684\u9ad8\u7ea7\u529f\u80fd\uff0c\u5982Hough\u53d8\u6362\u3001\u6a21\u677f\u5339\u914d\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import numpy as np<\/p>\n<p>def extract_coordinates(image_path):<\/p>\n<p>    # \u8bfb\u53d6\u56fe\u50cf<\/p>\n<p>    image = cv2.imread(image_path)<\/p>\n<p>    # \u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/p>\n<p>    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)<\/p>\n<p>    # \u4f7f\u7528\u970d\u592b\u53d8\u6362\u68c0\u6d4b\u76f4\u7ebf<\/p>\n<p>    edges = cv2.Canny(gray, 50, 150)<\/p>\n<p>    lines = cv2.HoughLinesP(edges, 1, np.pi\/180, threshold=100, minLineLength=50, maxLineGap=10)<\/p>\n<p>    # \u63d0\u53d6\u5750\u6807<\/p>\n<p>    coordinates = [((line[0], line[1]), (line[2], line[3])) for line in lines]<\/p>\n<p>    return coordinates<\/p>\n<p>image_path = &quot;path\/to\/your\/image.jpg&quot;<\/p>\n<p>print(extract_coordinates(image_path))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u8f93\u51fa\u56fe\u50cf\u4e2d\u6240\u6709\u76f4\u7ebf\u7684\u5750\u6807\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ecb\u7ecd\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u4f7f\u7528Python\u63d0\u53d6\u5750\u6807\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5305\u62ec<strong>\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u7b2c\u4e09\u65b9\u5730\u7406\u4fe1\u606f\u5e93\u3001\u56fe\u50cf\u5904\u7406\u5e93<\/strong>\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u5e0c\u671b\u672c\u6587\u7684\u4ecb\u7ecd\u80fd\u5bf9\u5927\u5bb6\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u63d0\u53d6CSV\u6587\u4ef6\u4e2d\u7684\u5750\u6807\u6570\u636e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u63d0\u53d6CSV\u6587\u4ef6\u4e2d\u7684\u5750\u6807\u6570\u636e\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u3002\u901a\u8fc7\u8bfb\u53d6CSV\u6587\u4ef6\u5e76\u9009\u62e9\u7279\u5b9a\u7684\u5217\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u63d0\u53d6\u5750\u6807\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>pd.read_csv()<\/code>\u51fd\u6570\u52a0\u8f7d\u6587\u4ef6\uff0c\u7136\u540e\u901a\u8fc7\u5217\u540d\u9009\u62e9\u7ecf\u5ea6\u548c\u7eac\u5ea6\u5217\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\n# \u8bfb\u53d6CSV\u6587\u4ef6\ndata = pd.read_csv(&#39;coordinates.csv&#39;)\n\n# \u63d0\u53d6\u7ecf\u5ea6\u548c\u7eac\u5ea6\nlongitude = data[&#39;longitude&#39;]\nlatitude = data[&#39;latitude&#39;]\n<\/code><\/pre>\n<p><strong>\u4f7f\u7528Python\u63d0\u53d6\u56fe\u50cf\u4e2d\u7684\u5750\u6807\u4fe1\u606f\u6709\u4ec0\u4e48\u65b9\u6cd5\uff1f<\/strong><br \/>\u5728\u56fe\u50cf\u5904\u7406\u4e2d\uff0c\u63d0\u53d6\u5750\u6807\u4fe1\u606f\u901a\u5e38\u4f7f\u7528OpenCV\u6216PIL\u5e93\u3002\u901a\u8fc7\u52a0\u8f7d\u56fe\u50cf\u5e76\u5229\u7528\u9f20\u6807\u4e8b\u4ef6\uff0c\u53ef\u4ee5\u83b7\u53d6\u7528\u6237\u70b9\u51fb\u7684\u5750\u6807\u3002\u4f8b\u5982\uff0c\u4f7f\u7528OpenCV\u7684<code>cv2.setMouseCallback()<\/code>\u51fd\u6570\u6765\u8bb0\u5f55\u70b9\u51fb\u4f4d\u7f6e\u3002\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import cv2\n\ndef get_coordinates(event, x, y, flags, param):\n    if event == cv2.EVENT_LBUTTONDOWN:\n        print(f&#39;\u5750\u6807: ({x}, {y})&#39;)\n\n# \u52a0\u8f7d\u56fe\u50cf\nimg = cv2.imread(&#39;image.jpg&#39;)\ncv2.imshow(&#39;Image&#39;, img)\ncv2.setMouseCallback(&#39;Image&#39;, get_coordinates)\n\ncv2.waitKey(0)\ncv2.destroyAllWindows()\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u4ece\u5730\u7406\u4fe1\u606f\u7cfb\u7edf\uff08GIS\uff09\u6570\u636e\u4e2d\u63d0\u53d6\u5750\u6807\uff1f<\/strong><br \/>\u5904\u7406GIS\u6570\u636e\u65f6\uff0c\u4f7f\u7528Geopandas\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u63d0\u53d6\u5750\u6807\u3002Geopandas\u5141\u8bb8\u7528\u6237\u8bfb\u53d6\u5404\u79cd\u5730\u7406\u6570\u636e\u683c\u5f0f\uff08\u5982Shapefile\u3001GeoJSON\u7b49\uff09\uff0c\u5e76\u8f7b\u677e\u8bbf\u95ee\u5750\u6807\u4fe1\u606f\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import geopandas as gpd\n\n# \u8bfb\u53d6GIS\u6570\u636e\ngdf = gpd.read_file(&#39;data.shp&#39;)\n\n# \u63d0\u53d6\u5750\u6807\ncoordinates = gdf.geometry.apply(lambda geom: geom.representative_point().coords[:])\ncoordinates_list = list(coordinates)\n<\/code><\/pre>\n<p>\u8fd9\u4e9b\u65b9\u6cd5\u63d0\u4f9b\u4e86\u4e0d\u540c\u573a\u666f\u4e0b\u63d0\u53d6\u5750\u6807\u7684\u6709\u6548\u89e3\u51b3\u65b9\u6848\uff0c\u9002\u5408\u4e8e\u6570\u636e\u5206\u6790\u3001\u56fe\u50cf\u5904\u7406\u548c\u5730\u7406\u4fe1\u606f\u5904\u7406\u7b49\u9886\u57df\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5f00\u5934\u6bb5\u843d: \u4f7f\u7528Python\u63d0\u53d6\u5750\u6807\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u7b2c\u4e09\u65b9\u5730\u7406\u4fe1\u606f\u5e93\u3001\u56fe\u50cf\u5904\u7406\u5e93\u7b49\u65b9\u5f0f\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1024544,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1024535"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1024535"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1024535\/revisions"}],"predecessor-version":[{"id":1024545,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1024535\/revisions\/1024545"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1024544"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1024535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1024535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1024535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}