在MATLAB中,我有一组纬度和经度对,代表美国的位置。我需要确定到最近海岸线的距离。
我认为MATLAB有一个内置的美国纬度/经度点数据库。我如何访问并使用它?
关于如何有效确定距离的任何建议?
答案 0 :(得分:8)
由于我无法访问Mapping Toolbox,这是解决此问题的理想选择,因此我想出了一个独立于任何工具箱的解决方案,包括Image Processing Toolbox 的
Steve Eddins有一个image processing blog at The MathWorks,去年他有一系列很酷的帖子致力于使用数字高程地图。具体来说,他指出了在哪里获取它们以及如何加载和处理它们。以下是相关的博文:
Locating the US continental divide, part 1 - Introduction:在这里,史蒂夫展示了您可以获取数字高程图(DEM)图块的位置以及如何加载和处理它们。您可以从tile E获取DEM图块(tile F和Global Land One-km Base Elevation Project覆盖美国大陆)。可以找到每个图块的纬度和经度范围here。
Locating the US continental divide, part 4 - Ocean masks:使用上述帖子中处理过的DEM数据,Steve展示了如何创建海洋面具。
利用这个DEM数据,您可以找到海洋边缘所在的纬度和经度,找到从内陆地图点到最近的海岸点的距离,然后进行甜蜜的可视化。我使用函数FILTER2来帮助通过与海洋掩模的卷积来找到海洋的边缘,以及用于计算great-circle distances以获得沿着地球表面的地图点之间的距离的等式。
使用上面博客文章中的一些示例代码,这是我想出的:
%# Load the DEM data:
data_size = [6000 10800 1]; %# The data has 1 band.
precision = 'int16=>int16'; %# Read 16-bit signed integers into a int16 array.
header_bytes = 0;
interleave = 'bsq'; %# Band sequential. Not critical for 1 band.
byte_order = 'ieee-le';
E = multibandread('e10g',data_size,precision,... %# Load tile E
header_bytes,interleave,byte_order);
F = multibandread('f10g',data_size,precision,... %# Load tile F
header_bytes,interleave,byte_order);
dem = [E F]; %# The digital elevation map for tile E and F
clear E F; %# Clear E and F (they are huge!)
%# Crop the DEM data and get the ranges of latitudes and longitudes:
[r,c] = size(dem); %# Size of DEM
rIndex = [1 4000]; %# Row range of DEM to keep
cIndex = [6000 14500]; %# Column range of DEM to keep
dem = dem(rIndex(1):rIndex(2),cIndex(1):cIndex(2)); %# Crop the DEM
latRange = (50/r).*(r-rIndex+0.5); %# Range of pixel center latitudes
longRange = (-180/c).*(c-cIndex+0.5); %# Range of pixel center longitudes
%# Find the edge points of the ocean:
ocean_mask = dem == -500; %# The ocean is labeled as -500 on the DEM
kernel = [0 1 0; 1 1 1; 0 1 0]; %# Convolution kernel
[latIndex,longIndex] = ... %# Find indices of points on ocean edge
find(filter2(kernel,~ocean_mask) & ocean_mask);
coastLat = latRange(1)+diff(latRange).*... %# Convert indices to
(latIndex-1)./diff(rIndex); %# latitude values
coastLong = longRange(1)+diff(longRange).*... %# Convert indices to
(longIndex-1)./diff(cIndex); %# longitude values
%# Find the distance to the nearest coastline for a set of map points:
lat = [39.1407 35 45]; %# Inland latitude points (in degrees)
long = [-84.5012 -100 -110]; %# Inland longitude points (in degrees)
nPoints = numel(lat); %# Number of map points
scale = pi/180; %# Scale to convert degrees to radians
radiusEarth = 3958.76; %# Average radius of Earth, in miles
distanceToCoast = zeros(1,nPoints); %# Preallocate distance measure
coastIndex = zeros(1,nPoints); %# Preallocate a coastal point index
for iPoint = 1:nPoints %# Loop over map points
rho = cos(scale.*lat(iPoint)).*... %# Compute central angles from map
cos(scale.*coastLat).*... %# point to all coastal points
cos(scale.*(coastLong-long(iPoint)))+...
sin(scale.*lat(iPoint)).*...
sin(scale.*coastLat);
d = radiusEarth.*acos(rho); %# Compute great-circle distances
[distanceToCoast(iPoint),coastIndex(iPoint)] = min(d); %# Find minimum
end
%# Visualize the data:
image(longRange,latRange,dem,'CDataMapping','scaled'); %# Display the DEM
set(gca,'DataAspectRatio',[1 1 1],'YDir','normal',... %# Modify some axes
'XLim',longRange,'YLim',fliplr(latRange)); %# properties
colormap([0 0.8 0.8; hot]); %# Add a cyan color to the "hot" colormap
xlabel('Longitude'); %# Label the x axis
ylabel('Latitude'); %# Label the y axis
hold on; %# Add to the plot
plot([long; coastLong(coastIndex).'],... %'# Plot the inland points and
[lat; coastLat(coastIndex).'],... %'# nearest coastal points
'wo-');
str = strcat(num2str(distanceToCoast.',... %'# Make text for the distances
'%0.1f'),{' miles'});
text(long,lat,str,'Color','w','VerticalAlignment','bottom'); %# Plot the text
这是最终的数字:
我想这让我离最近的“海洋”海岸线差不多400英里(实际上,它可能是Intracoastal Waterway)。
答案 1 :(得分:5)
load coast;
axesm('mercator');
plotm(lat,long)
在coast.mat所在的目录中还有其他数据集可能更有用。
然后,我会找到数据集中所有点的距离,并采用最短距离。这将假设美国以外的海岸线是可接受的答案。您将需要使用距离函数,因为欧几里德几何不适用于此处。答案 2 :(得分:4)
Gnovice的回答很好,对未来有用,但我不需要那么高的保真度,也不想花费额外的时间从像素距离转换到纬度/经度距离。以MatlabDoug的答案为例,我编写了以下脚本:
% Get Data
coast = load('coast.mat');
locations = load('locations.mat');
% Preallocate
coast_indexes = nan(size(locations.lat));
distancefromcoast = nan(size(locations.lat));
% Find distance and corresponding coastal point
for i=1:1:numel(locations.lat)
[dist, az] = distance(locations.lat(i), locations.long(i), coast.lat, coast.long);
[distancefromcoast(i),coast_indexes(i)] = min(dist);
end