Quantcast
Channel: DotSpatial Issue Tracker Rss Feed
Viewing all articles
Browse latest Browse all 1128

Commented Unassigned: ToRaster method overload proposal [25539]

$
0
0
Hi all (Maxim :P)

I needed to create an overload to the ToRaster method in Dotspatial.Analysis.VectorToRaster.cs.

The problem I was facing was that the existing versions of that method did not lead me to the results I needed. In particular I could not get exactly the number of rows and columns I wanted to get from the extent raster used to mask the featureset.

Anyway, maybe I messed up something, but it works now, so if you ever think to include it here is the code

```
public static IRaster ToRaster(IFeatureSet fs, double Xmin, double Ymin, int Ncols, int Nrows, double cellSize, string fieldName,
string outputFileName,
string driverCode, string[] options, IProgressHandler progressHandler)
{
Extent env = new Extent(Xmin, Ymin, Xmin + (Ncols - 1) * cellSize, Ymin + (Nrows - 1) * cellSize);

//Hopefully nobody will insert a cellSize equal to 0
//if (cellSize == 0)
//{
// if (Ncols < nrows)
// {
// cellSize = env.Width / 256;
// }
// else
// {
// cellSize = env.Height / 256;
// }
//}
int w = Ncols; // (int)Math.Ceiling(env.Width / cellSize);
if (w > 8000)
{
w = 8000;
cellSize = (cellSize*Ncols) / 8000;
}
int h = Nrows; // (int)Math.Ceiling(env.Height / cellSize);
if (h > 8000)
{
h = 8000;
}
Bitmap bmp = new Bitmap(w, h);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.Transparent);
g.SmoothingMode = SmoothingMode.None;
g.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
g.InterpolationMode = InterpolationMode.NearestNeighbor;
Hashtable colorTable;
MapArgs args = new MapArgs(new Rectangle(0, 0, w, h), env, g);

switch (fs.FeatureType)
{
case FeatureType.Polygon:
{
MapPolygonLayer mpl = new MapPolygonLayer(fs);
PolygonScheme ps = new PolygonScheme();
colorTable = ps.GenerateUniqueColors(fs, fieldName);
mpl.Symbology = ps;
mpl.DrawRegions(args, new List<Extent> { env });
}
break;
case FeatureType.Line:
{
MapLineLayer mpl = new MapLineLayer(fs);
LineScheme ps = new LineScheme();
colorTable = ps.GenerateUniqueColors(fs, fieldName);
mpl.Symbology = ps;
mpl.DrawRegions(args, new List<Extent> { env });
}
break;
default:
{
MapPointLayer mpl = new MapPointLayer(fs);
PointScheme ps = new PointScheme();
colorTable = ps.GenerateUniqueColors(fs, fieldName);
mpl.Symbology = ps;
mpl.DrawRegions(args, new List<Extent> { env });
}
break;
}
Type tp = fieldName == "FID" ? typeof(int) : fs.DataTable.Columns[fieldName].DataType;
// We will try to convert to double if it is a string
if (tp == typeof(string))
{
tp = typeof(double);
}
InRamImageData image = new InRamImageData(bmp, env);
ProgressMeter pm = new ProgressMeter(progressHandler, "Converting To Raster Cells", h);

IRaster output;
output = Raster.Create(outputFileName, driverCode, w, h, 1, tp, options);
output.Bounds = new RasterBounds(h, w, env);

double noDataValue = output.NoDataValue;

if (fieldName != "FID")
{
// We can't use this method to calculate Max on a non-existent FID field.
double dtMax = Convert.ToDouble(fs.DataTable.Compute("Max(" + fieldName + ")", ""));
double dtMin = Convert.ToDouble(fs.DataTable.Compute("Min(" + fieldName + ")", ""));

if (dtMin <= noDataValue && dtMax >= noDataValue)
{
if (dtMax != GetFieldValue(tp, "MaxValue"))
{
output.NoDataValue = noDataValue;
}
else if (dtMin != GetFieldValue(tp, "MinValue"))
{
output.NoDataValue = noDataValue;
}
}
}

List<RcIndex> locations = new List<RcIndex>();
List<string> failureList = new List<string>();
for (int row = 0; row < h; row++)
{
for (int col = 0; col < w; col++)
{
Color c = image.GetColor(row, col);
if (c.A == 0)
{
output.Value[row, col] = output.NoDataValue;
}
else
{
if (colorTable.ContainsKey(c) == false)
{
if (c.A < 125)
{
output.Value[row, col] = output.NoDataValue;
continue;
}

// Use a color matching distance to pick the closest member
object val = GetCellValue(w, h, row, col, image, c, colorTable, locations);

output.Value[row, col] = GetDouble(val, failureList);
}
else
{
output.Value[row, col] = GetDouble(colorTable[c], failureList);
}
}
}
pm.CurrentValue = row;
}
const int maxIterations = 5;
int iteration = 0;
while (locations.Count > 0)
{
List<RcIndex> newLocations = new List<RcIndex>();
foreach (RcIndex location in locations)
{
object val = GetCellValue(w, h, location.Row, location.Column, image,
image.GetColor(location.Row, location.Column), colorTable, newLocations);
output.Value[location.Row, location.Column] = GetDouble(val, failureList);
}
locations = newLocations;
iteration++;
if (iteration > maxIterations)
{
break;
}
}

pm.Reset();
return output;
}

```

Thanks
Oscar
Comments: ** Comment from web user: Oscarafone77 **

oh, I didn't think about that way.

Thank you,

I will try it this afternoon

Thanks
Oscar


Viewing all articles
Browse latest Browse all 1128

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>