14
14
using MapBoard . Model ;
15
15
using System . Collections . Concurrent ;
16
16
using FzLib . Collection ;
17
+ using static MapBoard . Util . GeometryUtility . CentripetalCatmullRom ;
17
18
18
19
namespace MapBoard . Util
19
20
{
@@ -397,7 +398,7 @@ private static double GetVerticleDistance(MapPoint p1, MapPoint p2, MapPoint pc)
397
398
private static double GetVerticleDistance ( Polyline line , MapPoint pc )
398
399
{
399
400
var nearestPoint = GeometryEngine . NearestCoordinate ( line , pc ) ;
400
- var dist = GeometryEngine . DistanceGeodetic ( pc , nearestPoint . Coordinate , LinearUnits . Meters , AngularUnits . Degrees , GeodeticCurveType . NormalSection ) ;
401
+ var dist = GeometryEngine . DistanceGeodetic ( pc , nearestPoint . Coordinate , LinearUnits . Meters , AngularUnits . Degrees , GeodeticCurveType . Geodesic ) ;
401
402
return dist . Distance ;
402
403
}
403
404
@@ -702,8 +703,9 @@ await Task.Run(() =>
702
703
/// <param name="features"></param>
703
704
/// <param name="pointsPerSegment">两个节点之间生成多少新点</param>
704
705
/// <param name="level">平滑等级,0最拟合,1一般,2最平滑</param>
706
+ /// <param name="minSmoothAngle">最小需要平滑的角度。若某三个节点组成的角小于该角度,那么会对中间点左右两侧分别进行平滑然后拼接</param>
705
707
/// <returns></returns>
706
- public static async Task Smooth ( IEditableLayerInfo layer , Feature [ ] features , int pointsPerSegment , int level )
708
+ public static async Task < IEnumerable < Feature > > Smooth ( IEditableLayerInfo layer , Feature [ ] features , int pointsPerSegment , int level , bool deleteOldFeature = false , double minSmoothAngle = 45 )
707
709
{
708
710
if ( level < 0 || level > 2 )
709
711
{
@@ -713,56 +715,45 @@ public static async Task Smooth(IEditableLayerInfo layer, Feature[] features, in
713
715
{
714
716
throw new ArgumentOutOfRangeException ( nameof ( pointsPerSegment ) ) ;
715
717
}
716
- //CentripetalCatmullRom中数量是算上头尾的
717
- pointsPerSegment += 2 ;
718
- ConcurrentBag < UpdatedFeature > updatedFeatures = new ConcurrentBag < UpdatedFeature > ( ) ;
718
+ ConcurrentBag < UpdatedFeature > updatedFeatures = null ;
719
+ ConcurrentBag < Feature > addedFeatures = null ;
720
+ if ( deleteOldFeature )
721
+ {
722
+ updatedFeatures = new ConcurrentBag < UpdatedFeature > ( ) ;
723
+ }
724
+ else
725
+ {
726
+ addedFeatures = new ConcurrentBag < Feature > ( ) ;
727
+ }
719
728
await Task . Run ( ( ) =>
720
729
{
721
730
Parallel . ForEach ( features , feature =>
722
731
{
723
- Geometry oldGeometry = feature . Geometry ;
724
- Geometry newGeometry = null ;
725
- if ( feature . Geometry is Multipart m )
732
+ var oldGeometry = feature . Geometry ;
733
+ var newGeometry = GeometryUtility . Smooth ( oldGeometry , pointsPerSegment , level , minSmoothAngle ) ; ;
734
+ if ( deleteOldFeature )
726
735
{
727
- List < IEnumerable < MapPoint > > newParts = new List < IEnumerable < MapPoint > > ( ) ;
728
- foreach ( var part in m . Parts )
729
- {
730
- if ( part . PointCount <= 2 )
731
- {
732
- newParts . Add ( part . Points ) ;
733
- }
734
- else
735
- {
736
- newParts . Add ( CentripetalCatmullRom . Interpolate ( part . Points . ToList ( ) , pointsPerSegment , ( CentripetalCatmullRom . CatmullRomType ) level ) ) ;
737
- }
738
- }
739
- switch ( feature . Geometry )
740
- {
741
- case Polyline line :
742
- newGeometry = new Polyline ( newParts ) ;
743
- break ;
744
-
745
- case Polygon polygon :
746
- newGeometry = new Polygon ( newParts ) ;
747
- break ;
748
- }
749
- }
750
- else if ( feature . Geometry is Multipoint mp )
751
- {
752
- if ( mp . Points . Count > 2 )
753
- {
754
- newGeometry = new Multipoint ( CentripetalCatmullRom . Interpolate ( mp . Points . ToList ( ) , pointsPerSegment , ( CentripetalCatmullRom . CatmullRomType ) level ) ) ;
755
- }
736
+ feature . Geometry = newGeometry ;
737
+ updatedFeatures . Add ( new UpdatedFeature ( feature , oldGeometry , feature . Attributes ) ) ;
756
738
}
757
739
else
758
740
{
759
- throw new NotSupportedException ( "仅支持多点、折线和多边形" ) ;
741
+ var newFeature = feature . Clone ( layer ) ;
742
+ newFeature . Geometry = newGeometry ;
743
+ addedFeatures . Add ( newFeature ) ;
760
744
}
761
- feature . Geometry = newGeometry ;
762
- updatedFeatures . Add ( new UpdatedFeature ( feature , oldGeometry , feature . Attributes ) ) ;
763
745
} ) ;
764
746
} ) ;
765
- await layer . UpdateFeaturesAsync ( updatedFeatures , FeatureOperation ) ;
747
+
748
+ if ( deleteOldFeature )
749
+ {
750
+ await layer . UpdateFeaturesAsync ( updatedFeatures , FeatureOperation ) ;
751
+ return updatedFeatures . Select ( p => p . Feature ) ;
752
+ }
753
+ else
754
+ {
755
+ return await layer . AddFeaturesAsync ( addedFeatures , FeatureOperation ) ;
756
+ }
766
757
}
767
758
768
759
/// <summary>
0 commit comments