我正在通过添加新的列属性来转换 shapefile。由于此任务是使用 Java 执行的,因此我目前知道的唯一选择是使用 Geotools。我有两个主要问题:
1. 我不知道如何实际添加新的列变量。feature.setAttribute("col","value") 是答案吗?
我从这篇文章中看到的只是示例:https : //gis.stackexchange.com/questions/215660/modifying-feature-attributes-of-a-shapefile-in-geotools,但我没有得到解决方案。
//Upload the ShapeFile
File file = JFileDataStoreChooser.showOpenFile("shp", null);
Map<String, Object> params = new HashMap<>();
params.put("url", file.toURI().toURL());
DataStore store = DataStoreFinder.getDataStore(params);
SimpleFeatureSource featureSource = store.getFeatureSource(store.getTypeNames()[0]);
String typeName = store.getTypeNames()[0];
FeatureSource<SimpleFeatureType, SimpleFeature> source =
store.getFeatureSource(typeName);
Filter filter = Filter.INCLUDE;
FeatureCollection<SimpleFeatureType, SimpleFeature> collection = source.getFeatures(filter);
try (FeatureIterator<SimpleFeature> features = collection.features()) {
while (features.hasNext()) {
SimpleFeature feature = features.next();
//adding new columns
feature.setAttribute("ShapeID", "SHP1213");
feature.setAttribute("UserName", "John");
System.out.print(feature.getID());
System.out.print(":");
System.out.println(feature.getDefaultGeometryProperty().getValue());
}
} …Run Code Online (Sandbox Code Playgroud) 关于这个主题的现有问题已经很少,但不幸的是我没有找到可以解决我的问题的东西。
我有一个点 Lat,Long 坐标,即 Lat= 10 和 Long = 10。我想围绕这个点创建一个 0.5 度边界框的形状文件,所以边界框应该如下:
有谁知道如何在 Python 中做到这一点?
我正在尝试读取 shapefile
r = shapefile.Reader(filepath, encoding = "utf-8")
Run Code Online (Sandbox Code Playgroud)
但是当我尝试从.records()对象获取值时,例如:
r.records()[0]
Run Code Online (Sandbox Code Playgroud)
它返回给我以下错误:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 4: invalid continuation byte
Run Code Online (Sandbox Code Playgroud) 我试图确保我正确安装了 geodjango,以便我可以按照本教程进行操作。
我使用 Brew 作为 Geodjango 先决条件的安装程序:postgresql、postgis、gdal、libgeoip。
我的 gdal 版本是最新的:3.1.2。但是,当我尝试使用ogrinfo world/data/TM_WORLD_BORDERS-0.3.shp检查 .shp 文件时,它会在控制台中返回此错误:
Unable to open datasource '[this is the name of the file here'] with the following drivers.然后它只列出一大堆驱动程序。
我一生都不知道该怎么做,类似问题的所有其他答案都建议升级 GDAL,但我的是最新版本......
我对 ggplot2 中的图例几乎没有疑问。如何从所附代码中连续制作图例。在此代码中,图例采用离散形式,我想将其设为连续形式(平滑连续模式)。代码是可重现的(有点长,但可能在此代码中一行会发生变化)
library(sf)
library(sp)
r <- raster(t((volcano[,ncol(volcano):1] - 94) * 4.95))
# Let's mock with a shapefile
poly <- st_as_sfc(st_bbox(st_as_sf(rasterToPolygons(r))))
# Sample 4 points
set.seed(3456)
sample <- st_sample(poly, 4)
sample <- st_buffer(sample, c(0.01, 0.02, 0.03))
sample <- st_sf(x=1:4, sample)
st_write(sample, "1aa.shp", append = FALSE)
# Mocked data
# Now let's start with code -------
r <- raster(t((volcano[,ncol(volcano):1] - 94) * 4.95))
# Use sf!!
pg <- st_read("1aa.shp") # loadshapfile
plot(r)
plot(st_geometry(pg), add= TRUE,) # it appears here …Run Code Online (Sandbox Code Playgroud) 如果您有以ESRI形状存储的地理数据,则至少有三个文件:一个以包含vector-data的.shp结尾,一个以包含属性的.dbf结尾,另一个以包含索引的.shx结尾的文件.
我对shx文件感兴趣.它是如何工作的?它是否包含完整的映射,例如"每个几何体的第一个几何图谱映射到dbf中的第三行,第二个几何图谱映射到第一行"?或者它的工作方式不同?
我有一个多边形(在Shapely对象中转换).我的目标是在图例子后面计算"内部质心"(也称为"表面上的点")(返回x,y值)和"质心"(返回x,y值):

from shapely.geometry import Polygon
ref_polygon = Polygon(points)
# get the x and y coordinate of the centroid
ref_polygon.centroid.wkt
'POINT (558768.9293489187300000 6361851.0362532493000000)'
Run Code Online (Sandbox Code Playgroud)
我的问题是一些程序员已经在Python中开发了一个函数来计算内部质心或知道一些模块来做到这一点.
提前致谢
使用的点(多边形的顶点)是:
points = [(560036.4495758876, 6362071.890493258),
(560036.4495758876, 6362070.890493258),
(560036.9495758876, 6362070.890493258),
(560036.9495758876, 6362070.390493258),
(560037.4495758876, 6362070.390493258),
(560037.4495758876, 6362064.890493258),
(560036.4495758876, 6362064.890493258),
(560036.4495758876, 6362063.390493258),
(560035.4495758876, 6362063.390493258),
(560035.4495758876, 6362062.390493258),
(560034.9495758876, 6362062.390493258),
(560034.9495758876, 6362061.390493258),
(560032.9495758876, 6362061.390493258),
(560032.9495758876, 6362061.890493258),
(560030.4495758876, 6362061.890493258),
(560030.4495758876, 6362061.390493258),
(560029.9495758876, 6362061.390493258),
(560029.9495758876, 6362060.390493258),
(560029.4495758876, 6362060.390493258),
(560029.4495758876, 6362059.890493258),
(560028.9495758876, 6362059.890493258),
(560028.9495758876, 6362059.390493258),
(560028.4495758876, 6362059.390493258),
(560028.4495758876, 6362058.890493258),
(560027.4495758876, 6362058.890493258),
(560027.4495758876, …Run Code Online (Sandbox Code Playgroud) 我正在为大伦敦地区提供一个形状文件.我使用包中的readShapePoly函数将maptools其作为一个加载到R中SpatialPolygonDataFrame.
我想绘制那些多边形..我已经通过使用plotR中的函数基本已经完成了.输出看起来如下图所示:

现在,我正在尝试绘制相同的形状文件,ggplot2但它不适用于我.我在图中看到一些奇怪的线,如图所示:

我使用的代码是:
london.wards <- readShapePoly("~/TD/london_wards2013/london_wards2013.shp"
, proj4string=CRS(projString))
wards.count <- nrow(london.wards@data)
# assign id for each lsoa
london.wards@data$id <- 1:wards.count
wards.fort <- fortify(london.wards, region='id')
ggplot(wards.fort, aes(long, lat)) + geom_polygon(colour='black', fill='white')
Run Code Online (Sandbox Code Playgroud)
其中projString是描述用于输入形状文件的投影的投影字符串.
我有一个shapefile要显示。我尝试使用matplotlib来显示它,但是我得到了:
但是,当我尝试使用在线网站进行展示时,我得到了;

如何获得第二张图像?
这是我的代码:
import shapefile
import matplotlib.pyplot as plt
print("Initializing Shapefile")
sf = shapefile.Reader("ap_abl")
apShapes = sf.shapes()
points = apShapes[3].points
print("Shapefile Initialized")
print("Initializing Display")
fig = plt.figure()
ax = fig.add_subplot(111)
plt.xlim([78, 79])
plt.ylim([19, 20])
print("Display Initialized")
print("Creating Polygon")
ap = plt.Polygon(points, fill=False, edgecolor="k")
ax.add_patch(ap)
print("Polygon Created")
print("Displaying polygon")
plt.show()
Run Code Online (Sandbox Code Playgroud)
先感谢您。
我在情节和图例上的正确颜色范围有问题。
这是我使用的代码:
data.ch4 <- read.csv2("v42_CH4_1970_TOT.txt",skip = 3,stringsAsFactors = FALSE, header = F)
num_data <- data.frame(data.matrix(data.ch4))
library(maptools)
library(lattice)
library(png)
#map loading
map1 <- readShapePoly("CNTR_2014_03M_SH/Data/CNTR_RG_03M_2014.shp")
coordinates(num_data) <- ~V2+V1
gridded(num_data) <- TRUE
#plotting
png(file="Map2.png",width=35,height=30,unit="cm", res=200, type = "cairo")
spplot(num_data["V3"], xlim=c(-5,35), ylim=c(35,70),
sp.layout = list("sp.polygons",map1),contour=F)
dev.off()
Run Code Online (Sandbox Code Playgroud)
这是带有数据的文件:https : //www.sendspace.com/file/hjtatp (压缩后通常重量为57 mb)
从此处开始的地图(但地图具有第二优先级,可以跳过)
所以一切都是蓝色的。显然,从最小到最大值都有很大的距离。我想固定比例,例如最后一个值应为“高于x”。我试图这样做:
所以现在看起来好多了。这是我的方法:
#Fixed breakpoints (?)
at <- c(0e+0, 1.5e-5, 1.0e-4, 1.0e-3, 1.0e-2, 1.0e-1, 1.0e+0, 2.0e+0, 1.0e+1, 1.0e+2, 2.0e+2,5.0e+2)
spplot(num_data["V3"], xlim=c(-5,35), ylim=c(35,70),
sp.layout = list("sp.polygons",map1),
contour=F,
at=at) #right there …Run Code Online (Sandbox Code Playgroud) shapefile ×10
python ×4
gis ×3
r ×3
ggplot2 ×2
r-maptools ×2
attributes ×1
console ×1
django ×1
gdal ×1
geodjango ×1
geolocation ×1
geometry ×1
geotools ×1
java ×1
legend ×1
matplotlib ×1
postgis ×1
python-3.x ×1
r-sp ×1
raster ×1
spatial ×1
unicode ×1