private static StringBuilder GetKmlDataBuilder(IEnumerable <FeatureLayer> featureLayers)
        {
            StringBuilder builder   = new StringBuilder();
            KmlGeoCanvas  kmlCanvas = new KmlGeoCanvas();

            Proj4Projection proj = new Proj4Projection();

            proj.InternalProjectionParametersString = GisEditor.ActiveMap.DisplayProjectionParameters;
            proj.ExternalProjectionParametersString = Proj4Projection.GetDecimalDegreesParametersString();
            proj.Open();

            kmlCanvas.BeginDrawing(builder, proj.ConvertToExternalProjection(GisEditor.ActiveMap.CurrentExtent), GeographyUnit.DecimalDegree);
            featureLayers.ForEach(l =>
            {
                Proj4ProjectionInfo projectionInfo = l.GetProj4ProjectionInfo();
                if (projectionInfo != null)
                {
                    projectionInfo.ExternalProjectionParametersString = Proj4Projection.GetWgs84ParametersString();
                    projectionInfo.SyncProjectionParametersString();
                }

                l.Open();
                l.Draw(kmlCanvas, new Collection <SimpleCandidate>());
            });
            kmlCanvas.EndDrawing();
            return(builder);
        }
Exemple #2
0
        protected void SaveInternalProj4ProjectionParameters(FeatureLayer featureLayer, string proj4ProjectionParameters)
        {
            Proj4ProjectionInfo projectionInfo = featureLayer.GetProj4ProjectionInfo();

            if (projectionInfo != null)
            {
                projectionInfo.InternalProjectionParametersString = proj4ProjectionParameters;
            }

            SaveInternalProj4ProjectionParametersCore(featureLayer, proj4ProjectionParameters);
        }
        public static void SaveKmlData(IEnumerable <FeatureLayer> featureLayers, KmlParameter parameter)
        {
            StringBuilder builder   = new StringBuilder();
            GeoCanvas     kmlCanvas = new KmlGeoCanvas();

            if (parameter.Is3DKml)
            {
                kmlCanvas = new Kml3DGeoCanvas();
                ((Kml3DGeoCanvas)kmlCanvas).ZHeight = parameter.ZHeight;
            }
            Proj4Projection proj = new Proj4Projection();

            proj.InternalProjectionParametersString = GisEditor.ActiveMap.DisplayProjectionParameters;
            proj.ExternalProjectionParametersString = Proj4Projection.GetDecimalDegreesParametersString();
            proj.Open();

            kmlCanvas.BeginDrawing(builder, proj.ConvertToExternalProjection(GisEditor.ActiveMap.CurrentExtent), GeographyUnit.DecimalDegree);
            featureLayers.ForEach(l =>
            {
                Proj4ProjectionInfo projectionInfo = l.GetProj4ProjectionInfo();
                if (projectionInfo != null)
                {
                    projectionInfo.ExternalProjectionParametersString = Proj4Projection.GetWgs84ParametersString();
                    projectionInfo.SyncProjectionParametersString();
                }

                l.Open();
                l.Draw(kmlCanvas, new Collection <SimpleCandidate>());
            });
            kmlCanvas.EndDrawing();
            using (StreamWriter sw = new StreamWriter(parameter.PathFileName))
            {
                sw.Write(builder.ToString());
                sw.Close();
            }
        }
Exemple #4
0
        private IEnumerable <Feature> StandardClip(FeatureLayer featureLayer, IEnumerable <Feature> features)
        {
            lock (featureLayer)
            {
                Collection <Feature> results = new Collection <Feature>();

                #region replace project to null

                bool isOpen = false;
                Proj4ProjectionInfo projectionInfo = featureLayer.GetProj4ProjectionInfo();
                if (projectionInfo != null && projectionInfo.CanProject)
                {
                    if (featureLayer.IsOpen)
                    {
                        featureLayer.Close();
                        projectionInfo.Close();
                        isOpen = true;
                    }
                    featureLayer.FeatureSource.Projection = null;
                }

                #endregion replace project to null

                Collection <Feature> tmpFeatures = new Collection <Feature>();
                if (projectionInfo != null && projectionInfo.CanProject)
                {
                    projectionInfo.Open();
                    foreach (var item in features)
                    {
                        tmpFeatures.Add(projectionInfo.ConvertToInternalProjection(item));
                    }
                    projectionInfo.Close();
                }
                else
                {
                    tmpFeatures = new Collection <Feature>(features.ToList());
                }

                if (!featureLayer.IsOpen)
                {
                    featureLayer.Open();
                }
                List <Feature> tmpSourceFeatures = featureLayer.FeatureSource.GetFeaturesInsideBoundingBox(ExtentHelper.GetBoundingBoxOfItems(tmpFeatures), featureLayer.GetDistinctColumnNames()).Select(f => f.MakeValidIfCan()).ToList();

                Collection <Feature> sourceFeatures = new Collection <Feature>(tmpSourceFeatures);
                if (projectionInfo != null)
                {
                    featureLayer.FeatureSource.Projection = projectionInfo.Projection;
                    if (isOpen)
                    {
                        featureLayer.Open();
                    }
                }

                SimpleShapeType simpleShapeType = SimpleShapeType.Unknown;
                var             shapeAdapter    = GisEditor.LayerManager.GetLayerPlugins(featureLayer.GetType()).FirstOrDefault() as FeatureLayerPlugin;
                if (shapeAdapter != null)
                {
                    simpleShapeType = shapeAdapter.GetFeatureSimpleShapeType(featureLayer);
                }
                if (featureLayer.IsOpen)
                {
                    featureLayer.Close();
                }

                int index = 1;
                int count = sourceFeatures.Count;
                if (simpleShapeType == SimpleShapeType.Point)
                {
                    return(StandardClipPoints(sourceFeatures, tmpFeatures));
                }
                else if (simpleShapeType == SimpleShapeType.Line)
                {
                    StandardClipLines(tmpFeatures, results, sourceFeatures, index, count);
                }
                else if (simpleShapeType == SimpleShapeType.Area)
                {
                    //MultipolygonShape areaBaseShape = AreaBaseShape.Union(GetValidFeatures(tmpFeatures));
                    List <AreaBaseShape> clippingAreaShapes = GetValidFeatures(tmpFeatures)
                                                              .Select(f => f.GetShape())
                                                              .OfType <AreaBaseShape>()
                                                              .ToList();

                    BaseShape         unionResultShape = SqlTypesGeometryHelper.Union(clippingAreaShapes);
                    MultipolygonShape areaBaseShape    = ConvertSqlQueryResultToMultiPolygonShape(unionResultShape);

                    foreach (var feature in sourceFeatures)
                    {
                        isCanceled = ReportProgress(index, count);
                        if (isCanceled)
                        {
                            break;
                        }

                        try
                        {
                            index++;
                            //if (areaBaseShape.Contains(feature))
                            if (SqlTypesGeometryHelper.Contains(areaBaseShape, feature))
                            {
                                results.Add(feature);
                            }
                            else
                            {
                                //var clippedShape = areaBaseShape.GetIntersection(feature);
                                AreaBaseShape targetAreaShape = feature.GetShape() as AreaBaseShape;
                                if (targetAreaShape != null)
                                {
                                    var clippedShape = SqlTypesGeometryHelper.GetIntersection(areaBaseShape, targetAreaShape) as MultipolygonShape;
                                    if (clippedShape != null && clippedShape.Polygons.Count > 0)
                                    {
                                        results.Add(new Feature(clippedShape.GetWellKnownBinary(), feature.Id, feature.ColumnValues));
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            GisEditor.LoggerManager.Log(LoggerLevel.Debug, ex.Message, new ExceptionInfo(ex));
                            HandleExceptionFromInvalidFeature(feature.Id, ex.Message);
                        }
                    }
                }
                else
                {
                    throw new NotSupportedException("The ShapeFileType is not supported.");
                }

                Collection <Feature> convertedFeatures = new Collection <Feature>();
                if (projectionInfo != null && projectionInfo.CanProject)
                {
                    projectionInfo.Open();
                    foreach (var item in results)
                    {
                        convertedFeatures.Add(projectionInfo.ConvertToExternalProjection(item));
                    }
                    projectionInfo.Close();
                }
                else
                {
                    convertedFeatures = new Collection <Feature>(results.ToList());
                }

                return(convertedFeatures);
            }
        }
Exemple #5
0
        private IEnumerable <Feature> InverseClip(FeatureLayer featureLayer, IEnumerable <Feature> clippingFeatures)
        {
            lock (featureLayer)
            {
                if (!featureLayer.IsOpen)
                {
                    featureLayer.Open();
                }
                Collection <Feature> results         = featureLayer.FeatureSource.GetFeaturesOutsideBoundingBox(ExtentHelper.GetBoundingBoxOfItems(clippingFeatures), featureLayer.GetDistinctColumnNames());
                Collection <Feature> sourceFeatures  = new Collection <Feature>();
                SimpleShapeType      simpleShapeType = GisEditor.LayerManager.GetFeatureSimpleShapeType(featureLayer);
                int index = 1;
                if (simpleShapeType == SimpleShapeType.Point)
                {
                    featureLayer.Open();
                    Collection <Feature> allFeatures = featureLayer.FeatureSource.GetAllFeatures(featureLayer.GetDistinctColumnNames());
                    featureLayer.Close();
                    foreach (Feature f in results)
                    {
                        allFeatures.Remove(f);
                    }
                    foreach (var f in InverseClipPoints(allFeatures, clippingFeatures, simpleShapeType))
                    {
                        results.Add(f);
                    }
                }
                else if (simpleShapeType == SimpleShapeType.Line)
                {
                    bool isOpen = false;
                    Proj4ProjectionInfo projectionInfo = featureLayer.GetProj4ProjectionInfo();
                    //MultipolygonShape areaBaseShape = AreaBaseShape.Union(GetValidFeatures(clippingFeatures));
                    List <AreaBaseShape> clippingAreaShapes = GetValidFeatures(clippingFeatures)
                                                              .Select(f => f.GetShape())
                                                              .OfType <AreaBaseShape>()
                                                              .ToList();
                    MultipolygonShape areaBaseShape = AreaBaseShape.Union(clippingAreaShapes);

                    if (projectionInfo != null && projectionInfo.CanProject)
                    {
                        if (featureLayer.IsOpen)
                        {
                            featureLayer.Close();
                            projectionInfo.Close();
                            isOpen = true;
                        }
                        featureLayer.FeatureSource.Projection = null;
                    }
                    featureLayer.Open();
                    featureLayer.FeatureSource.GetFeaturesInsideBoundingBox(areaBaseShape.GetBoundingBox(), featureLayer.GetDistinctColumnNames()).ForEach(f => { if (!areaBaseShape.Contains(f))
                                                                                                                                                                  {
                                                                                                                                                                      sourceFeatures.Add(f);
                                                                                                                                                                  }
                                                                                                                                                           });
                    int count = sourceFeatures.Count;
                    if (projectionInfo != null)
                    {
                        featureLayer.FeatureSource.Projection = projectionInfo.Projection;
                        if (isOpen)
                        {
                            featureLayer.Open();
                        }
                    }
                    if (featureLayer.IsOpen)
                    {
                        featureLayer.Close();
                    }
                    foreach (var feature in sourceFeatures)
                    {
                        isCanceled = ReportProgress(index, count);
                        if (isCanceled)
                        {
                            break;
                        }

                        index++;
                        try
                        {
                            //if (areaBaseShape.IsDisjointed(feature))
                            if (SqlTypesGeometryHelper.IsDisjointed(areaBaseShape, feature))
                            {
                                results.Add(feature);
                            }
                            else
                            {
                                MultilineShape multiLine   = (MultilineShape)feature.GetShape();
                                MultilineShape resultShape = new MultilineShape();
                                foreach (LineShape lineShape in multiLine.Lines)
                                {
                                    //if (areaBaseShape.IsDisjointed(lineShape))
                                    if (SqlTypesGeometryHelper.IsDisjointed(areaBaseShape, lineShape))
                                    {
                                        resultShape.Lines.Add(lineShape);
                                    }
                                    else
                                    {
                                        var resultLine = lineShape.GetDifference(areaBaseShape);
                                        foreach (var line in resultLine.Lines)
                                        {
                                            resultShape.Lines.Add(line);
                                        }
                                    }
                                }
                                if (resultShape != null && resultShape.Lines.Count > 0)
                                {
                                    results.Add(new Feature(resultShape.GetWellKnownBinary(), feature.Id, feature.ColumnValues));
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            GisEditor.LoggerManager.Log(LoggerLevel.Debug, ex.Message, new ExceptionInfo(ex));
                            HandleExceptionFromInvalidFeature(feature.Id, ex.Message);
                        }
                    }
                }
                else if (simpleShapeType == SimpleShapeType.Area)
                {
                    //MultipolygonShape areaBaseShape = AreaBaseShape.Union(GetValidFeatures(clippingFeatures));
                    List <AreaBaseShape> clippingAreaShapes = GetValidFeatures(clippingFeatures)
                                                              .Select(f => f.GetShape())
                                                              .OfType <AreaBaseShape>()
                                                              .ToList();

                    BaseShape         unionResultShape = SqlTypesGeometryHelper.Union(clippingAreaShapes);
                    MultipolygonShape areaBaseShape    = ConvertSqlQueryResultToMultiPolygonShape(unionResultShape);

                    bool isOpen = false;
                    Proj4ProjectionInfo projectionInfo = featureLayer.GetProj4ProjectionInfo();
                    if (projectionInfo != null && projectionInfo.CanProject)
                    {
                        if (featureLayer.IsOpen)
                        {
                            featureLayer.Close();
                            if (projectionInfo != null)
                            {
                                projectionInfo.Close();
                            }
                            isOpen = true;
                        }
                        featureLayer.FeatureSource.Projection = null;
                    }
                    if (!featureLayer.IsOpen)
                    {
                        featureLayer.Open();
                    }
                    featureLayer.FeatureSource.GetFeaturesInsideBoundingBox(areaBaseShape.GetBoundingBox(), featureLayer.GetDistinctColumnNames()).ForEach(f => sourceFeatures.Add(f));
                    if (featureLayer.IsOpen)
                    {
                        featureLayer.Close();
                    }
                    if (projectionInfo != null)
                    {
                        featureLayer.FeatureSource.Projection = projectionInfo.Projection;
                        if (isOpen)
                        {
                            featureLayer.Open();
                        }
                    }

                    int count = sourceFeatures.Count;
                    foreach (var feature in sourceFeatures)
                    {
                        isCanceled = ReportProgress(index, count);
                        if (isCanceled)
                        {
                            break;
                        }

                        index++;
                        try
                        {
                            //if (areaBaseShape.IsDisjointed(feature))
                            if (SqlTypesGeometryHelper.IsDisjointed(areaBaseShape, feature))
                            {
                                results.Add(feature);
                            }
                            else
                            {
                                //var clippedShape = ((AreaBaseShape)feature.GetShape()).GetDifference(areaBaseShape);
                                BaseShape         differenceResultShape = SqlTypesGeometryHelper.GetDifference((AreaBaseShape)feature.GetShape(), areaBaseShape);
                                MultipolygonShape clippedShape          = ConvertSqlQueryResultToMultiPolygonShape(differenceResultShape);
                                if (clippedShape != null && clippedShape.Polygons.Count > 0)
                                {
                                    results.Add(new Feature(clippedShape.GetWellKnownBinary(), feature.Id, feature.ColumnValues));
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            GisEditor.LoggerManager.Log(LoggerLevel.Debug, ex.Message, new ExceptionInfo(ex));
                            HandleExceptionFromInvalidFeature(feature.Id, ex.Message);
                        }
                    }
                }
                else
                {
                    throw new NotSupportedException("The ShapeFileType is not supported.");
                }
                return(results);
            }
        }
        public static void SaveToKmlMenuitemClick(object sender, RoutedEventArgs e)
        {
            Collection <Feature> features = GisEditor.SelectionManager.GetSelectedFeatures();

            if (features.All(i => !GisEditor.ActiveMap.CurrentExtent.Intersects(i)))
            {
                ShowOptionsIfNoSelectedFeatureInCurrentExtent(features);
                return;
            }

            Collection <FeatureLayer> featureLayers = new Collection <FeatureLayer>();
            var             featuresGroup           = GisEditor.SelectionManager.GetSelectionOverlay().GetSelectedFeaturesGroup();
            Proj4Projection tempProjection          = new Proj4Projection();

            tempProjection.InternalProjectionParametersString = GisEditor.ActiveMap.DisplayProjectionParameters;
            tempProjection.ExternalProjectionParametersString = Proj4Projection.GetWgs84ParametersString();
            tempProjection.SyncProjectionParametersString();

            try
            {
                tempProjection.Open();
                foreach (var item in featuresGroup)
                {
                    if (item.Value.Count > 0)
                    {
                        InMemoryFeatureLayer layer = new InMemoryFeatureLayer();
                        ZoomLevelSet         sourceZoomLevelSet = item.Key.ZoomLevelSet;
                        try
                        {
                            string       tempXml            = GisEditor.Serializer.Serialize(sourceZoomLevelSet);
                            ZoomLevelSet targetZoomLevelSet = (ZoomLevelSet)GisEditor.Serializer.Deserialize(tempXml);
                            layer.ZoomLevelSet = targetZoomLevelSet;

                            layer.Open();
                            layer.EditTools.BeginTransaction();
                            foreach (var feature in item.Value)
                            {
                                Feature newFeature = tempProjection.ConvertToExternalProjection(feature);
                                layer.EditTools.Add(newFeature);
                            }
                            layer.EditTools.CommitTransaction();

                            if (!item.Key.IsOpen)
                            {
                                item.Key.Open();
                            }
                            foreach (var column in item.Key.QueryTools.GetColumns())
                            {
                                layer.Columns.Add(column);
                            }

                            featureLayers.Add(layer);
                        }
                        catch (Exception ex)
                        {
                            GisEditor.LoggerManager.Log(LoggerLevel.Debug, ex.Message, new ExceptionInfo(ex));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                GisEditor.LoggerManager.Log(LoggerLevel.Debug, ex.Message, new ExceptionInfo(ex));
            }
            finally
            {
                tempProjection.Close();
            }

            KmlParameter parameter = GetKmlParameter();

            //System.Windows.Forms.SaveFileDialog sf = new System.Windows.Forms.SaveFileDialog();
            //sf.Filter = "(*.kml)|*.kml|(*.kmz)|*.kmz";
            //sf.FileName = string.Format("{0}-{1}", "KmlExportFile", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"));
            if (parameter != null)
            {
                if (Path.GetExtension(parameter.PathFileName).ToUpper() == ".KML")
                {
                    SaveKmlData(featureLayers, parameter);
                }
                else if (Path.GetExtension(parameter.PathFileName).ToUpper() == ".KMZ")
                {
                    StringBuilder         builder       = GetKmlDataBuilder(featureLayers);
                    PlatformGeoCanvas     canvas        = new PlatformGeoCanvas();
                    System.Drawing.Bitmap bitmap        = new System.Drawing.Bitmap((int)GisEditor.ActiveMap.ActualWidth, (int)GisEditor.ActiveMap.ActualHeight);
                    RectangleShape        drawingExtent = GetDrawingExtentInWgs84();
                    canvas.BeginDrawing(bitmap, drawingExtent, GeographyUnit.DecimalDegree);
                    featureLayers.ForEach(l =>
                    {
                        Proj4ProjectionInfo projectionInfo = l.GetProj4ProjectionInfo();
                        if (projectionInfo != null)
                        {
                            projectionInfo.ExternalProjectionParametersString = Proj4Projection.GetWgs84ParametersString();
                            projectionInfo.SyncProjectionParametersString();
                        }

                        l.Open();
                        l.Draw(canvas, new Collection <SimpleCandidate>());
                    });
                    canvas.EndDrawing();
                    string kmlPath = Path.ChangeExtension(parameter.PathFileName, ".kml");
                    string pngPath = Path.ChangeExtension(parameter.PathFileName, ".png");

                    if (File.Exists(kmlPath))
                    {
                        File.Delete(kmlPath);
                    }
                    if (File.Exists(pngPath))
                    {
                        File.Delete(pngPath);
                    }

                    MemoryStream stream = new MemoryStream();
                    bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                    byte[] bitmapArray = stream.GetBuffer();

                    File.WriteAllText(kmlPath, builder.ToString());
                    File.WriteAllBytes(pngPath, bitmapArray);

                    var zipFileAdapter = ZipFileAdapterManager.CreateInstance();
                    zipFileAdapter.AddFileToZipFile(kmlPath, "");
                    zipFileAdapter.AddFileToZipFile(pngPath, "");
                    zipFileAdapter.Save(Path.ChangeExtension(parameter.PathFileName, ".kmz"));
                    File.Delete(kmlPath);
                    File.Delete(pngPath);
                }

                string mentionedString = GisEditor.LanguageManager.GetStringResource("KMLFileHasSavedSuccessText");
                if (MessageBox.Show(mentionedString, "Open in Google Earth", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    string proInstalledPath = GetGoogleEarthProInstalledPath();
                    OpenKmlFileWithGoogleEarth(string.IsNullOrEmpty(proInstalledPath) ? GetGoogleEarthInstalledPath() : proInstalledPath
                                               , parameter.PathFileName);
                }
            }
        }