private static Bitmap GenerateBitmap(IRegularGridCoverageLayer gridLayer, IRegularGridCoverage grid, PixelFormat pixelFormat, IVariableFilter[] filters, int xWidth, int yWidth) { /*var xWidth = * ((VariableIndexRangesFilter) filters[0]).IndexRanges[0].Second-((VariableIndexRangesFilter) filters[0]). * IndexRanges[0].First +1; * var yWidth = ((VariableIndexRangesFilter)filters[1]).IndexRanges[0].Second - ((VariableIndexRangesFilter)filters[1]). * IndexRanges[0].First+1;*/ var bitmap = new Bitmap(xWidth, yWidth, pixelFormat); if (grid.Components[0].ValueType == typeof(double)) { FillBitmap <double>(gridLayer, grid, bitmap, pixelFormat, filters); } else if (grid.Components[0].ValueType == typeof(float)) { FillBitmap <float>(gridLayer, grid, bitmap, pixelFormat, filters); } else if (grid.Components[0].ValueType == typeof(int)) { FillBitmap <int>(gridLayer, grid, bitmap, pixelFormat, filters); } else if (grid.Components[0].ValueType == typeof(short)) { FillBitmap <short>(gridLayer, grid, bitmap, pixelFormat, filters); } else if (grid.Components[0].ValueType == typeof(UInt32)) { FillBitmap <UInt32>(gridLayer, grid, bitmap, pixelFormat, filters); } else if (grid.Components[0].ValueType == typeof(Byte)) { //todo hack: in case source file is tiff it has its own color //return bitmap; FillBitmap <Byte>(gridLayer, grid, bitmap, pixelFormat, filters); } else { log.WarnFormat("Cant render type: " + grid.Components[0].ValueType); } return(bitmap); }
public GdalRenderer(IRegularGridCoverageLayer layer) : base(layer) { }
public RegularGridCoverageRenderer(IRegularGridCoverageLayer layer) { RegularGridCoverageLayer = layer; }
protected static void FillBitmap <T>(IRegularGridCoverageLayer gridLayer, IRegularGridCoverage gridCoverage, Bitmap bitmap, PixelFormat pixelFormat, IVariableFilter[] filters) where T : IComparable, IComparable <T> { // HACK: free memory before getting large block GC.Collect(1, GCCollectionMode.Optimized); T[] gridValues = gridCoverage.Components[0].GetValues <T>(filters).ToArray(); var imageWidth = bitmap.Width; var imageHeight = bitmap.Height; Trace.Assert(imageHeight * imageWidth == gridValues.Count()); //flip upside down for (int k = 0; k < imageWidth; k++) { for (int j = 0; j < imageHeight / 2; j++) { T swap1 = gridValues[k + j * imageWidth]; T swap2 = gridValues[(imageHeight - 1 - j) * imageWidth + k]; gridValues[k + j * imageWidth] = swap2; gridValues[(imageHeight - 1 - j) * imageWidth + k] = swap1; } } if (gridLayer.Theme == null) { object minValue = gridCoverage.Components[0].MinValue; object maxValue = gridCoverage.Components[0].MaxValue; gridLayer.Theme = GenerateDefaultTheme(gridCoverage.Components[0].Name, gridCoverage.Components[0].NoDataValues, minValue, maxValue); } // Add NoDataValues from grid coverage if it was unspecified: var theme = gridLayer.Theme as Theme; if (theme != null && theme.NoDataValues == null) { theme.NoDataValues = gridCoverage.Components[0].NoDataValues; } BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, imageWidth, imageHeight), ImageLockMode.ReadWrite, pixelFormat); try { unsafe { // use a pointer to get direct access to the bits in the bitmap. // this is faster and removes the need to copy values from // unmanaged to managed memory and vice versa. IntPtr array = bitmapData.Scan0; int * pByte = (int *)array.ToPointer(); int bytes = bitmapData.Width * bitmapData.Height; ((Theme)gridLayer.Theme).GetFillColors(pByte, bytes, (T[])gridValues); } } catch (Exception e) { throw e; } finally { if (bitmapData != null) { bitmap.UnlockBits(bitmapData); // force a collect to reclaim large allocated arrays that have survived the first generations GC.Collect(1, GCCollectionMode.Optimized); } } }
/// <summary> /// Sony NS @ 2013-08-06 /// </summary> /// <param name="worldPos"></param> /// <param name="limit"></param> /// <param name="outLayer"></param> /// <param name="condition"></param> /// <returns></returns> public IFeature FindNearestFeature(ICoordinate worldPos, float limit, out ILayer outLayer, Func <ILayer, bool> condition) { IFeature nearestFeature = null; outLayer = null; // Since we are only interested in one geometry object start with the topmost trackersLayer and stop // searching the lower layers when an object is found. // Sony NS @ 2013-08-06, order list ILayer Desc, karena untuk layer-layer symbol diload paling terakhir foreach (ILayer mapLayer in Map.GetAllVisibleLayers(true).Reverse()) { if (mapLayer is DiscreteGridPointCoverageLayer) { try { var curvilinearGridLayer = (DiscreteGridPointCoverageLayer)mapLayer; var coverage = (IDiscreteGridPointCoverage)curvilinearGridLayer.Coverage; var nearestFeatures = coverage.GetFeatures(worldPos.X, worldPos.Y, limit); if (nearestFeatures != null) { if (!curvilinearGridLayer.ShowFaces) { nearestFeatures = nearestFeatures.Where(f => !(f is IGridFace)); } if (!curvilinearGridLayer.ShowVertices) { nearestFeatures = nearestFeatures.Where(f => !(f is IGridVertex)); } } nearestFeature = nearestFeatures.FirstOrDefault(); outLayer = curvilinearGridLayer; } catch (Exception) { // GetCoordinateAtPosition will throw exception if x, y is not within the coverage } } if (mapLayer is VectorLayer) { var vectorLayer = mapLayer as VectorLayer; IEnvelope envelope; float localLimit = limit; if ((!vectorLayer.IsSelectable) || ((null != condition) && (!condition(vectorLayer)))) { continue; } // Adjust the marge limit for Layers with a symbol style and if the size of the symbol exceeds // the minimum limit. Ignore layers with custom renderers if ((vectorLayer.Style.Symbol != null) && (0 == vectorLayer.CustomRenderers.Count)) { ICoordinate size = MapHelper.ImageToWorld(MapControl.Map, vectorLayer.Style.Symbol.Width, vectorLayer.Style.Symbol.Height); if ((size.X > localLimit) || (size.Y > localLimit)) { envelope = MapHelper.GetEnvelope(worldPos, size.X, size.Y); localLimit = (float)Math.Max(envelope.Width, envelope.Height); } else { envelope = GetEnvelope(worldPos, localLimit); } } else { envelope = GetEnvelope(worldPos, localLimit); } IFeatureProvider featureProvider = vectorLayer.DataSource; if (null != featureProvider) { // Get features in the envelope IList objectsAt; if (vectorLayer.CustomRenderers.Count > 0) { objectsAt = vectorLayer.CustomRenderers[0].GetFeatures(envelope, vectorLayer); } else { objectsAt = featureProvider.GetFeatures(envelope); } // Mousedown at new position if (null != objectsAt) { IFeature feature = null; if (objectsAt.Count == 1) { feature = objectsAt.OfType <IFeature>().First(); } else if (objectsAt.Count > 1) { double localDistance; feature = FindNearestFeature(vectorLayer, objectsAt, worldPos, localLimit, out localDistance); } if (null != feature) { nearestFeature = feature; outLayer = vectorLayer; break; } } } } else if (mapLayer is IRegularGridCoverageLayer) { try { IRegularGridCoverageLayer regularGridCoverageLayer = (IRegularGridCoverageLayer)mapLayer; IRegularGridCoverage regularGridCoverage = (IRegularGridCoverage)regularGridCoverageLayer.Coverage; nearestFeature = regularGridCoverage.GetRegularGridCoverageCellAtPosition(worldPos.X, worldPos.Y); outLayer = regularGridCoverageLayer; } catch (Exception) { // GetCoordinateAtPosition will throw exception if x, y is not within the coverage } } } return(nearestFeature); }
public IFeature FindNearestFeature(ICoordinate worldPos, float limit, out ILayer outLayer, Func <ILayer, bool> condition) { IFeature nearestFeature = null; outLayer = null; // Since we are only interested in one geometry object start with the topmost trackersLayer and stop // searching the lower layers when an object is found. foreach (ILayer mapLayer in MapHelper.GetAllMapLayers(Map.Layers, false)) { if (mapLayer is VectorLayer) { var vectorLayer = mapLayer as VectorLayer; IEnvelope envelope; float localLimit = limit; if ((!vectorLayer.IsSelectable) || ((null != condition) && (!condition(vectorLayer)))) { continue; } // Adjust the marge limit for Layers with a symbol style and if the size of the symbol exceeds // the minimum limit. Ignore layers with custom renderers if ((vectorLayer.Style.Symbol != null) && (0 == vectorLayer.CustomRenderers.Count)) { ICoordinate size = MapControlHelper.ImageToWorld(MapControl.Map, vectorLayer.Style.Symbol.Width, vectorLayer.Style.Symbol.Height); if ((size.X > localLimit) || (size.Y > localLimit)) { envelope = MapControlHelper.GetEnvelope(worldPos, size.X, size.Y); localLimit = (float)Math.Max(envelope.Width, envelope.Height); } else { envelope = GetEnvelope(worldPos, localLimit); } } else { envelope = GetEnvelope(worldPos, localLimit); } IFeatureProvider featureProvider = vectorLayer.DataSource; if (null != featureProvider) { // Get features in the envelope IList objectsAt; if (vectorLayer.CustomRenderers.Count > 0) { objectsAt = vectorLayer.CustomRenderers[0].GetFeatures(envelope, vectorLayer); } else { objectsAt = featureProvider.GetFeatures(envelope); } // Mousedown at new position if ((null != objectsAt) && (objectsAt.Count > 0)) { double localDistance; IFeature feature = FindNearestFeature(vectorLayer, objectsAt, worldPos, localLimit, out localDistance); if (null != feature) { nearestFeature = feature; outLayer = vectorLayer; break; } } } } else if (mapLayer is IRegularGridCoverageLayer) { try { IRegularGridCoverageLayer regularGridCoverageLayer = (IRegularGridCoverageLayer)mapLayer; IRegularGridCoverage regularGridCoverage = (IRegularGridCoverage)regularGridCoverageLayer.Coverage; nearestFeature = regularGridCoverage.GetRegularGridCoverageCellAtPosition(worldPos.X, worldPos.Y); outLayer = regularGridCoverageLayer; } catch (Exception) { // GetCoordinateAtPosition will throw exception if x, y is not within the coverage } } } return(nearestFeature); }