Как я могу построить массив с условиями на контурной карте?

Получает список всех изображений из папки и подпапок, а также заботится о длинном исключении имени файла в Windows.

// To handle long folder names Pri external library is used.
// Source https://github.com/peteraritchie/LongPath    
using Directory = Pri.LongPath.Directory;
using DirectoryInfo = Pri.LongPath.DirectoryInfo;
using File = Pri.LongPath.File;
using FileInfo = Pri.LongPath.FileInfo;
using Path = Pri.LongPath.Path;

// Directory and sub directory search function
 public void DirectoryTree(DirectoryInfo dr, string searchname)
        {
            FileInfo[] files = null;
            var allFiles = new List<FileInfo>();
            try
            {
                files = dr.GetFiles(searchname);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            if (files != null)
            {
                try
                {
                    foreach (FileInfo fi in files)
                    {
                        allFiles.Add(fi);

                        string fileName = fi.DirectoryName + "\\" + fi.Name;
                        string orgFile = fileName;
                    }
                    var subDirs = dr.GetDirectories();

                    foreach (DirectoryInfo di in subDirs)
                    {
                        DirectoryTree(di, searchname);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

            }
        }

   public List<String> GetImagesPath(String folderName)
    {
       var dr = new DirectoryInfo(folderName);
       string ImagesExtensions = "jpg,jpeg,jpe,jfif,png,gif,bmp,dib,tif,tiff";
       string[] imageValues = ImagesExtensions.Split(',');
       List<String> imagesList = new List<String>();

                foreach (var type in imageValues)
                {
                    if (!string.IsNullOrEmpty(type.Trim()))
                    {
                        DirectoryTree(dr, "*." + type.Trim());
                        // output to list 
                       imagesList.Add = DirectoryTree(dr, "*." + type.Trim());
                    }

                }
         return imagesList;
  }
0
задан glibdud 7 March 2019 в 19:41
поделиться

1 ответ

Я генерирую набор данных для контурного графика на карте Cartopy. Точки данных для контурной обработки разделены на 2 группы с отрицательными и положительными значениями z. Numpy maskedarray используется в этой операции. Я надеюсь, что это полезно для общих читателей, в том числе ОП.

import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.ticker as mticker
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
import pandas as pd

from numpy.random import uniform, seed
from matplotlib.mlab import griddata   
# TODO, use newer scipy.interpolate() instead of `griddata`
import numpy.ma as ma

# make up some data around long,lat: (90, 18)
seed(0)
npts = 200
x0, y0 = 90, 18   # center of map in (long, lat), degrees
x = x0+uniform(-2, 2, npts)
y = y0+uniform(-2, 2, npts)
#z = x*np.exp(-x**2 - y**2)
z = (x-x0)*np.exp(-(x-x0)**2 - (y-y0)**2)  # elevation in meters

# define grid, for points interpolation from the made-up data above
gridx, gridy = 50,50
xi = x0+np.linspace(-2.1, 2.1, gridx)
yi = y0+np.linspace(-2.1, 2.1, gridy)

# interpolate for gridded data of (gridx, gridy) 
zi = griddata(x, y, z, xi, yi, interp='linear')
# xi.shape, yi.shape, zi.shape  => ((50,), (50,), (50, 50))

xig,yig = np.meshgrid(xi, yi)

# projection
useproj = ccrs.PlateCarree()
fig = plt.figure(figsize = (9, 7))
rect = [0.05, 0.05, 0.95, 0.95]  # for map extent
ax = fig.add_axes( rect, projection=useproj )

# contour the gridded data, plotting dots at the nonuniform data points.
CS = ax.contour(xig, yig, zi, 15, linewidths=0.5, colors='k')
CS = ax.contourf(xig, yig, zi, 15,
                  vmax=abs(zi).max(), vmin=-abs(zi).max())
plt.colorbar(CS)  # draw colorbar

# prep points for scatterplot of the gridded points
# make 2 masked-arrays, based on `zi`
mag = ma.masked_greater(zi, 0)  # mask points with +ve zi values
mal = ma.masked_less(zi, 0)     # mask points with -ve zi values

# apply masking to xig,yig; borrowing mask from mag
xig_greater_masked = ma.MaskedArray(xig, mask=mag.mask)  # must have compatible values
yig_greater_masked = ma.MaskedArray(yig, mask=mag.mask)

# apply masking to xig,yig; borrowing mask from mal
xig_less_masked = ma.MaskedArray(xig, mask=mal.mask)
yig_less_masked = ma.MaskedArray(yig, mask=mal.mask)

# for points with -ve z values (result of .masked_greater)
plt.scatter(xig_greater_masked, yig_greater_masked, s=3, color="w", \
            alpha=1, zorder=15, label="masked_greater z")
# for points with +ve z values (result of .masked_less)
ax.scatter(xig_less_masked, yig_less_masked, s=3, color="r", alpha=1, \
           zorder=15, label="masked_less z") 

leg = ax.legend(title='Masked z', framealpha=1.0, facecolor="lightgray")
leg.set_zorder(20)

gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True,
                  linewidth=2, color='gray', alpha=0.5, linestyle='--')

gl.xlabels_top = False
gl.ylabels_right = False

gl.xformatter = LONGITUDE_FORMATTER
gl.yformatter = LATITUDE_FORMATTER
gl.xlabel_style = {'size': 15, 'color': 'gray'}
#gl.xlabel_style = {'color': 'gray', 'weight': 'bold'}

plt.title('Masked data plot on contour')
plt.show()

Результирующий сюжет:

enter image description here

0
ответ дан swatchai 7 March 2019 в 19:41
поделиться
Другие вопросы по тегам:

Похожие вопросы: