@@ -582,51 +582,78 @@ define([
582
582
* </p>
583
583
*
584
584
* @param {Geometry } geometry The geometry to modify.
585
+ * @param {String } attributeName The name of the attribute.
586
+ * @param {String } attributeName3D The name of the attribute in 3D.
587
+ * @param {String } attributeName2D The name of the attribute in 2D.
585
588
* @param {Object } [projection=new GeographicProjection()] The projection to use.
586
589
*
587
590
* @returns {Geometry } The modified <code>geometry</code> argument with <code>position3D</code> and <code>position2D</code> attributes.
588
591
*
589
592
* @exception {DeveloperError} geometry is required.
593
+ * @exception {DeveloperError} attributeName is required.
594
+ * @exception {DeveloperError} attributeName3D is required.
595
+ * @exception {DeveloperError} attributeName2D is required.
596
+ * @exception {DeveloperError} geometry must have attribute matching the attributeName argument.
597
+ * @exception {DeveloperError} The attribute componentDatatype must be ComponentDatatype.DOUBLE.
590
598
*
591
599
* @example
592
- * geometry = GeometryPipeline.projectTo2D(geometry);
600
+ * geometry = GeometryPipeline.projectTo2D(geometry, 'position', 'position3D', 'position2D' );
593
601
*/
594
- GeometryPipeline . projectTo2D = function ( geometry , projection ) {
602
+ GeometryPipeline . projectTo2D = function ( geometry , attributeName , attributeName3D , attributeName2D , projection ) {
595
603
if ( ! defined ( geometry ) ) {
596
604
throw new DeveloperError ( 'geometry is required.' ) ;
597
605
}
598
606
599
- if ( defined ( geometry . attributes . position ) ) {
600
- projection = ( defined ( projection ) ) ? projection : new GeographicProjection ( ) ;
601
- var ellipsoid = projection . getEllipsoid ( ) ;
607
+ if ( ! defined ( attributeName ) ) {
608
+ throw new DeveloperError ( 'attributeName is required.' ) ;
609
+ }
602
610
603
- // Project original positions to 2D.
604
- var positions3D = geometry . attributes . position . values ;
605
- var projectedPositions = new Float64Array ( positions3D . length ) ;
606
- var index = 0 ;
611
+ if ( ! defined ( attributeName3D ) ) {
612
+ throw new DeveloperError ( 'attributeName3D is required.' ) ;
613
+ }
607
614
608
- for ( var i = 0 ; i < positions3D . length ; i += 3 ) {
609
- var position = Cartesian3 . fromArray ( positions3D , i , scratchProjectTo2DCartesian3 ) ;
610
- var lonLat = ellipsoid . cartesianToCartographic ( position , scratchProjectTo2DCartographic ) ;
611
- var projectedLonLat = projection . project ( lonLat , scratchProjectTo2DCartesian3 ) ;
615
+ if ( ! defined ( attributeName2D ) ) {
616
+ throw new DeveloperError ( 'attributeName2D is required.' ) ;
617
+ }
612
618
613
- projectedPositions [ index ++ ] = projectedLonLat . x ;
614
- projectedPositions [ index ++ ] = projectedLonLat . y ;
615
- projectedPositions [ index ++ ] = projectedLonLat . z ;
616
- }
619
+ var attribute = geometry . attributes [ attributeName ] ;
620
+ if ( ! defined ( attribute ) ) {
621
+ throw new DeveloperError ( 'geometry must have attribute matching the attributeName argument: ' + attributeName + '.' ) ;
622
+ }
617
623
618
- // Rename original positions to WGS84 Positions.
619
- geometry . attributes . position3D = geometry . attributes . position ;
624
+ if ( attribute . componentDatatype . value !== ComponentDatatype . DOUBLE . value ) {
625
+ throw new DeveloperError ( 'The attribute componentDatatype must be ComponentDatatype.DOUBLE.' ) ;
626
+ }
620
627
621
- // Replace original positions with 2D projected positions
622
- geometry . attributes . position2D = new GeometryAttribute ( {
623
- componentDatatype : ComponentDatatype . DOUBLE ,
624
- componentsPerAttribute : 3 ,
625
- values : projectedPositions
626
- } ) ;
627
- delete geometry . attributes . position ;
628
+ projection = ( defined ( projection ) ) ? projection : new GeographicProjection ( ) ;
629
+ var ellipsoid = projection . getEllipsoid ( ) ;
630
+
631
+ // Project original values to 2D.
632
+ var values3D = attribute . values ;
633
+ var projectedValues = new Float64Array ( values3D . length ) ;
634
+ var index = 0 ;
635
+
636
+ for ( var i = 0 ; i < values3D . length ; i += 3 ) {
637
+ var value = Cartesian3 . fromArray ( values3D , i , scratchProjectTo2DCartesian3 ) ;
638
+ var lonLat = ellipsoid . cartesianToCartographic ( value , scratchProjectTo2DCartographic ) ;
639
+ var projectedLonLat = projection . project ( lonLat , scratchProjectTo2DCartesian3 ) ;
640
+
641
+ projectedValues [ index ++ ] = projectedLonLat . x ;
642
+ projectedValues [ index ++ ] = projectedLonLat . y ;
643
+ projectedValues [ index ++ ] = projectedLonLat . z ;
628
644
}
629
645
646
+ // Rename original cartesians to WGS84 cartesians.
647
+ geometry . attributes [ attributeName3D ] = attribute ;
648
+
649
+ // Replace original cartesians with 2D projected cartesians
650
+ geometry . attributes [ attributeName2D ] = new GeometryAttribute ( {
651
+ componentDatatype : ComponentDatatype . DOUBLE ,
652
+ componentsPerAttribute : 3 ,
653
+ values : projectedValues
654
+ } ) ;
655
+ delete geometry . attributes [ attributeName ] ;
656
+
630
657
return geometry ;
631
658
} ;
632
659
0 commit comments