EXTRACT LAT LONG

EXTRACT LAT LONG

vjohnson8HQZZ
Enthusiast Enthusiast
439 Views
4 Replies
Message 1 of 5

EXTRACT LAT LONG

vjohnson8HQZZ
Enthusiast
Enthusiast

I have this following code which extracts x,y coordinates of a Block in the model space.
Could someone please help to know how I can extract the Lat Long of these coordinates, given that Geolocation is already set up in the drawing.

Thanks in advance.

public class Class1
{
    [CommandMethod("TEST")]
    public static void Test()
    {
        var doc = Application.DocumentManager.MdiActiveDocument;
        var db = doc.Database;
        var ed = doc.Editor;

        var peo = new PromptEntityOptions("\nSelect Block A: ");
        peo.SetRejectMessage("\nSelected object is not a block.");
        peo.AddAllowedClass(typeof(BlockReference), true);
        var per = ed.GetEntity(peo);
        if (per.Status != PromptStatus.OK)
            return;

        using (var tr = db.TransactionManager.StartTransaction())
        {
            var br = (BlockReference)tr.GetObject(per.ObjectId, OpenMode.ForRead);
            var btr = (BlockTableRecord)tr.GetObject(br.BlockTableRecord, OpenMode.ForRead);
            foreach (ObjectId id in btr)
            {
                if (id.ObjectClass.Name == "AcDbBlockReference")
                {
                    var nested = (BlockReference)tr.GetObject(id, OpenMode.ForRead);
                    var position = nested.Position.TransformBy(br.BlockTransform);
                    ed.WriteMessage($"\nPosition of block '{nested.Name}': {position}");
                    break;
                }
            }
            tr.Commit();
        }
    }

}
0 Likes
Accepted solutions (2)
440 Views
4 Replies
Replies (4)
Message 2 of 5

Norman_Yuan
Mentor
Mentor
Accepted solution
...
        using (var tr = db.TransactionManager.StartTransaction())
        {
			var geoData=(GeoLocationData)tr.GetObject(db.GeoDataObject, OpenMode.ForRead);
			
            var br = (BlockReference)tr.GetObject(per.ObjectId, OpenMode.ForRead);
            var btr = (BlockTableRecord)tr.GetObject(br.BlockTableRecord, OpenMode.ForRead);
            foreach (ObjectId id in btr)
            {
                if (id.ObjectClass.Name == "AcDbBlockReference")
                {
                    var nested = (BlockReference)tr.GetObject(id, OpenMode.ForRead);
                    var position = nested.Position.TransformBy(br.BlockTransform);
                    ed.WriteMessage($"\nPosition of block '{nested.Name}': {position}");
					
					var lonlatInfo=geoData.TransformToLonLatAlt(position);
					
					ed.WriteMessage($"\Long: {lonlatInfo.Longitude}, Latitude: {lonlatInfo.Latitude}");
					
                    break;
                }
            }
			
            tr.Commit();
        }
...

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 5

Gepaha
Collaborator
Collaborator

For me lonlatInfo is returned as Point3d, which is in geographic coordinates. There is no option to get the values ​​in lonlatInfo.Longitude and lonlatInfo.Latitude format.

Point3d geoPt = geoData.TransformToLonLatAlt(wcsPt);


GeoData.png

Only GeoDataLonLatAltInfo contains Latitude, Longitude.

GeoDataLonLatAltInfo geoPt1 = geoData.TransformToLonLatAlt(geoPt.X, geoPt.Y, geoPt.Z);

GeoData1.png

0 Likes
Message 4 of 5

Norman_Yuan
Mentor
Mentor
Accepted solution

@Gepaha 

You are right: with the GeoLocation's API, it is 2-step call the 2 overloaded TransformToLonLatAlt() methods with original drawing's Point3d to eventually get Lon/Lat. I confused it with a custom extension method I used, which combines the 2 steps into one.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 5 of 5

vjohnson8HQZZ
Enthusiast
Enthusiast

Thank you so much, @Norman_Yuan , @Gepaha 
The code you provide works well.

 

0 Likes