I'm having a string of coordinates with [Lat, Lng]
format.
How can I convert it to [Lng, Lat]
format in C#?
For example:
Input:
string LatLng = "[[12.06, 106.67],[12.67, 106.68], ... ]" //string
Output:
var LatLng = "[[106.67, 12.06],[106.68, 12.07], ... ]"
Start by splitting the string into parts each containing a number (as string):
string LatLng = "[[12.06, 106.67],[12.67, 106.68],[11, 22]]";
string[] parts =
LatLng.Split(new[] { '[', ']', ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
Then create pairs of numbers as ValueTuples:
var coordinates = Enumerable.Range(0, parts.Length / 2)
.Select(i => (parts[2 * i], parts[2 * i + 1]));
Finally, join these number pairs as inverted to form new coordinates:
string mapBoxCoords =
"[" + String.Join(", ", coordinates.Select(c => $"[{c.Item2}, {c.Item1}]")) + "]";
If you want to do more than just create a new string, then convert the coordinates to an appropriate data structure and don't work with strings. Eg you could declare a coordinate as
public struct Coordinate
{
public Coordinate(decimal latitude, decimal longitude)
{
Latitude = latitude;
Longitude = longitude;
}
public decimal Latitude { get; }
public decimal Longitude { get; }
public string ToLatLngString() => $"[{Latitude}, {Longitude}]";
public string ToLngLatString() => $"[{Longitude }, {Latitude}]";
public static IEnumerable<Coordinate> FromLatLngFormat(string latLng)
{
string[] parts = latLng.Split(
new[] { '[', ']', ',', ' ' },
StringSplitOptions.RemoveEmptyEntries);
return Enumerable.Range(0, parts.Length / 2)
.Select(i => new Coordinate(Decimal.Parse(parts[2 * i]),
Decimal.Parse(parts[2 * i + 1])));
}
}
and then do the conversion with
var coordinates = Coordinate.FromLatLngFormat(LatLng);
string mapBoxCoords =
"[" + String.Join(",", coordinates.Select(c => c.ToLngLatString())) + "]";
Now you can do a lot of other useful things with the coordinates.
When you're manipulating a set of data for a specific purpose, it's often the case that using a class to manage it makes the rest of the work easier. If you're doing anything more than just re-arranging the string, implementing something like this is usually the best way to go (NOTE: this is a simplified example and obviously does very little data-integrity/safety testing beyond null data, and range checking, but the string parsing should be fairly resilient:):
class LatLong {
// Properties
//
protected decimal _lat = 0.0M;
protected decimal _long = 0.0M;
// Facilitates validating that strings contain values between -180.0.. and 180.0...
protected static const string PATTERN = /* language=regex */
@"^[-+]?(0?[\d]{1,2}([.][\d]*)?|1[0-7][\d]([.][\d]*)?|180([.]0*)?)$";
// Accessors
//
public decimal Latitude
{
get => _lat;
set => _lat = Math.Max( Math.Min( 180.0M, value ), -180.0M );
}
public decimal Longitude
{
get => _long;
set => _long = Math.Max( Math.Min( 180.0M, value ), -180.0M );
}
// Methods
//
public LatLong Swap() => new LatLong() { Longitude = _long, Latitude = _lat };
public override string ToString() => $"[{Latitude}, {Longitude}]";
public static LatLong Parse( string data )
{
// This function expects data in the form of "###.##, ###.##"
// with any amount of other non-numeric data sprinkled in. As long as there
// are two decimal values ranging between -180.0 and +180.0, and
// separated by a comma, this should be able to parse it.
if (string.IsNullOrWhiteSpace( data )
throw new ArgumentException( "Invalid data!" );
// remove any cruft and split the values
string[] work = Regex.Replace( data, @"[^-.\d,]", "" ).Split( new char[] { ',' } );
LatLong result = new LatLong();
if (work.Length > 0)
result._lat = IsValidValue( work[ 0 ] ) ? decimal.Parse( work[ 0 ] ) : 0.00M;
if (work.Length > 1)
result._long = IsValidValue( work[ 1 ] ) ? decimal.Parse( work[ 1 ] ) : 0.00M;
return result;
}
public static bool IsValidValue( string test ) =>
!string.IsNullOrWhiteSpace( test ) && Regex.IsMatch( test.Trim(), PATTERN, RegexOptions.ExplicitCapture );
}
To do what you want, simply parse your source data through that class and use the .Swap()
function:
string LatLng = "[[12.06, 106.67],[12.67, 106.68], ... ]", output = "[";
foreach( string s in LatLng.Split( "],[", StringSplitOptions.RemoveEmptyEntries ) )
output += ((output.Length > 1) ? "," : "") + LatLong.Parse( s ).Swap().ToString();
output += "]";
First you need to trim off the open and closed square brackets and split the string by ],[ to get each set of coordinates.
Then you loop over each of those and split by comma to separate the lat and long. To make things easier, create a simple data container for those values. Since they go back to string you can make those values as strings, in my example I parse to float.
After you have all of your elements properly parsed out, you can reconstruct the string again.
private class LatLong
{
public LatLong(decimal lat, decimal @long)
{
Lat = lat;
Long = @long;
}
public decimal Lat { get; set; }
public decimal Long { get; set; }
}
private void SplitLatLong()
{
var originalString = "[[24.5, 111.232],[51.2, 112.3],[12, 54.1]]";
var outputList = new List<LatLong>();
var itemsSplit = originalString
.TrimStart(new char[] { '[' })
.TrimEnd(new char[] { ']' })
.Split(new string[] { "],[" }, StringSplitOptions.RemoveEmptyEntries);
foreach (var item in itemsSplit)
{
var latLongSplit = item
.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
outputList.Add(new LatLong(
decimal.Parse(latLongSplit[1]),
decimal.Parse(latLongSplit[0]))
);
}
var remerged = "[[" + string.Join("],[", outputList.Select(o => o.Lat + ", " + o.Long)) + "]]";
// Output the value to make sure it was correct (i am in winforms atm)
//
MessageBox.Show(originalString + "\r\n" + remerged);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.