小编Mar*_*erc的帖子

在 Sentry 中对“.format()”样式的日志消息进行分组

我使用SentryHandlerfromraven.handlers.logging来跟踪 Sentry 中任何更高级别的日志。我的日志消息是使用 动态填充的自定义内容.format(),因此文本消息本身不一定始终具有相同的内容。例如:

import logging
from raven.handlers.logging import SentryHandler
from raven.conf import setup_logging

# Create a "basic" logger
logger = logging.getLogger("root")

# Create a Sentry logger handler
sh = SentryHandler("https://******@sentry.io/******")
sh.setLevel(logging.WARNING)
setup_logging(sh)

# Send the desired message to Sentry via logger
if SomeInteresetingWarning():
    logger.warning("{} missing files in {} directiories!".format(num_files,num_dirs))
Run Code Online (Sandbox Code Playgroud)

一切都很好,只是这会导致每条独特的消息都被视为独特的警告,这当然不是真的。

GitHub 上有一个很好的QA涵盖了这个问题,但那里提供的解决方案仅适用于使用老式风格 格式化的字符串%s

有谁知道如何实现正确的 Sentry 消息分组(聚合),而不必重新设计字符串格式从format()%s占位符?

python format logging sentry

6
推荐指数
1
解决办法
1349
查看次数

指定Geopandas对象的所需crs格式

我使用geopandasto_file()方法将shapefile读入一个geopandas对象.shapefile具有.prj带ESRI WKT样式投影信息的有效文件:

PROJCS["Slovenia_1996_Slovene_National_Grid",GEOGCS["GCS_Slovenia 1996",DATUM["D_Slovenia_Geodetic_Datum_1996",SPHEROID["GRS_1980",6378137,298.257222101]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",15],PARAMETER["scale_factor",0.9999],PARAMETER["false_easting",500000],PARAMETER["false_northing",-5000000],UNIT["Meter",1]]
Run Code Online (Sandbox Code Playgroud)

有了这个,创建的地理数据框将crs属性设置为字典,与proj4字符串或epsg代码相比,我觉得很难使用它:

{u'lon_0': 15, u'k': 0.9999, u'ellps': u'GRS80', u'y_0': -5000000, u'no_defs': True, u'proj': u'tmerc', u'x_0': 500000, u'units': u'm', u'lat_0': 0}
Run Code Online (Sandbox Code Playgroud)

Geopandas 投影文档清楚地表明该.crs方法接受许多不同形式的crs信息(epsg代码,字典,proj4字符串......),但似乎在将geopandas写入shapefile时无法控制所需的格式.

问题: 有没有办法指定所需的crs格式或任何内置方法来切换crs属性的不同格式?

python shapefile fiona geopandas

5
推荐指数
1
解决办法
671
查看次数

将坐标系统导出为ESPG代码:to_epsg()或ExportToEPSG()

使用fiona和使用Python处理坐标系时osgeo,似乎有很多方法可以通过导入/导出不同的crs格式来定义坐标系,例如:

FIONA:

from fiona.crs import from_epsg,from_string,to_string

# Import crs from different formats:
wgs = from_epsg(4326)
wgs = from_string("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs ")

# Export crs as proj4 string
wgs_proj4_string = to_string(wgs)
Run Code Online (Sandbox Code Playgroud)

OSGEO:

from osgeo import osr

srs = osr.SpatialReference()
srs.ImportFromESRI(['GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]'])
srs.ImportFromProj4("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
srs.ImportFromEPSG(4326)
#the import options are very rich

# Export to different formats
srs.ExportToProj4()
srs.ExportToWkt()
srs.ExportToXML()
#... many export options!
Run Code Online (Sandbox Code Playgroud)

但是,我注意到,这两个库都允许通过其EPSG代码轻松定义crs,但是它们都缺少逆函数(将crs导出为ESPG代码)。

我最接近EPSG代码的方式是:

srs.AutoIdentifyEPSG()
epsg = srs.GetAuthorityCode(None)
Run Code Online (Sandbox Code Playgroud)

但是它似乎并不那么可靠,而且其他提议的解决方案似乎也包括大量的调整或至少与Web服务相关。

问题: …

python geospatial osgeo epsg fiona

5
推荐指数
1
解决办法
563
查看次数

使用 GeoDataFrame 作为 osgeo.ogr 数据源

我已将 shapefile 读入 aGeoDataFrame并对其进行了一些修改:

import geopandas as gpd

# Read shapefile into geodataframe
geodf = gpd.read_file("shapefile.shp")

# Do some "pandas-like" modifications to shapefile
geodf = modify_geodf(geodf)
Run Code Online (Sandbox Code Playgroud)

但是,我还想osgeo.ogr在其上应用模块的一些功能:

from osgeo import ogr

# Read shapefile into ogr DataSource
ds=ogr.Open("shapefile.shp")

# Do some "gdal/ogr-like" modifications to shapefile
ds = modify_ds(ds)
Run Code Online (Sandbox Code Playgroud)

问题:有什么方法可以直接使用或转换已在内存中的 shapefile,目前采用 GeoDataFrame 的形式osgeo.ogr.DataSource

到目前为止,我这样做的方法是将 GeoDataFrame 保存到文件中to_file(),然后osgeo.ogr.Open()再次保存,但这对我来说似乎有些多余。

python ogr geopandas

4
推荐指数
1
解决办法
1124
查看次数

Python子进程没有属性“check_output”

我的代码使用subprocess.check_output得很好,我不得不重新安装 Ubuntu 16.04,然后它抱怨它现在找不到属性 check_output。

import subprocess

p = subprocess.check_output("here is a command", shell=True)

/usr/bin/python2.7 /home/username/subprocess.py
Traceback (most recent call last):
  File "/home/username/subprocess.py", line 1, in <module>
    import subprocess
  File "/home/username/subprocess.py", line 4, in <module>
    p = subprocess.check_output("here is a command", shell=True)
AttributeError: 'module' object has no attribute 'check_output'
Run Code Online (Sandbox Code Playgroud)

请注意,我使用的是 Python2.7,根据这里的这篇文章应该可以解决这个问题,但事实并非如此。

subprocess.check_output() 模块对象有属性“check_output”

是什么赋予了?我尝试了 pip installsubprocess或 pip uninstallsubprocess但没有运气。如何更新subprocess到最新版本以使其具有该check_output属性?我不想使用Popen.

python subprocess python-2.7

4
推荐指数
1
解决办法
2048
查看次数

熊猫按组反转列值

我想在我的数据框中反转列值,但只能在单个“groupby”级别上。您可以在下面找到一个最小的演示示例,我想在其中“翻转”属于相同字母 A、B 或 C 的值:

df = pd.DataFrame({"group":["A","A","A","B","B","B","B","C","C"],
                   "value": [1,3,2,4,4,2,3,2,5]})

  group  value
0     A      1
1     A      3
2     A      2
3     B      4
4     B      4
5     B      2
6     B      3
7     C      2
8     C      5
Run Code Online (Sandbox Code Playgroud)

我想要的输出看起来像这样:(为了简洁起见,添加列而不是替换列)

  group  value  value_desired
0     A      1              2
1     A      3              3
2     A      2              1
3     B      4              3
4     B      4              2
5     B      2              4
6     B      3              4
7     C      2              5
8     C      5              2 …
Run Code Online (Sandbox Code Playgroud)

python sorting dataframe pandas columnsorting

3
推荐指数
1
解决办法
1870
查看次数

在 Geopandas 中绘图时管理投影

我正在使用 geopandas 绘制意大利地图。

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize = (20,30))

region_map.plot(ax=ax, color='white', edgecolor='black')
plt.xlim([6,19])
plt.ylim([36,47.7])
plt.tight_layout()
plt.show()
Run Code Online (Sandbox Code Playgroud)

region_map这就是正确定义为一块“几何” GeoSeries后的结果。

意大利地图

但是,我无法修改图形的纵横比,甚至无法figsize改变plt.subplots。我是否错过了一些微不足道的事情,或者可能是 geopandas 问题?

谢谢

python figure map-projections pandas geopandas

2
推荐指数
1
解决办法
3715
查看次数

GeoPandas .to_crs() 方法未转换

我正在尝试将一些数据集转换为相同的坐标系(NC2264)

NC2264 = 'EPSG:2264'

sfd_subs = pd.read_csv(r'FILE_LOCATION.csv')
wake_shapes = gpd.GeoDataFrame.from_file(r'FILE_LOCATION.shp').to_crs(NC2264)

sfd_subs = gpd.GeoDataFrame(sfd_subs, geometry=gpd.points_from_xy(sfd_subs.Longitude, sfd_subs.Latitude),crs='EPSG:4326')
sfd_subs.to_crs(NC2264)

print(sfd_subs.crs)
Run Code Online (Sandbox Code Playgroud)

shapefile 转换工作完美,但“sfd_subs”保持不变。我没有收到任何错误。

我在顶部添加了 5 个正确的转换,而在底部添加了未更改的转换。

EPSG:4326
0       POINT (2641914.208 1398556.771)
1       POINT (2642559.277 1398183.388)
2       POINT (2641705.300 1398352.924)
3       POINT (2641716.844 1397826.942)
4       POINT (2674989.747 1419749.281)
                     ...               
3332             POINT (-78.135 35.506)
3333             POINT (-78.130 35.504)
3334             POINT (-78.123 35.530)
3335             POINT (-78.104 35.537)
3336             POINT (-78.087 35.562)
Run Code Online (Sandbox Code Playgroud)

因为我没有收到任何错误,所以我不确定最好的行动方案是什么。

python coordinate-systems coordinate-transformation pandas geopandas

2
推荐指数
1
解决办法
2716
查看次数

使用单独的自定义分类(阈值)对Pandas列进行分类

我有dataframe一些数值存储在"值"列中,并附有各自的分类阈值(在这种情况下为警告级别),存储在其他列中(在我的情况下为"低","中间","高"):

     value    low  middle    high
0   179.69  17.42   88.87  239.85
1     2.58  17.81   93.37  236.58
2     1.21   0.05    0.01    0.91
3     1.66   0.20    0.32    4.57
4     3.54   0.04    0.04    0.71
5     5.97   0.16    0.17    2.55
6     5.39   0.86    1.62    9.01
7     1.20   0.03    0.01    0.31
8     3.19   0.08    0.01    0.45
9     0.02   0.03    0.01    0.10
10    3.98   0.18    0.05    0.83
11  134.51  78.63  136.86  478.27
12  254.53  83.73  146.33  486.65 …
Run Code Online (Sandbox Code Playgroud)

python binning dataframe pandas

1
推荐指数
1
解决办法
534
查看次数