Skip to content

Instantly share code, notes, and snippets.

@bchavez
Last active February 11, 2022 17:56
Show Gist options
  • Save bchavez/dd1a1e044738a4d8028b0abdfd7d6ad3 to your computer and use it in GitHub Desktop.
Save bchavez/dd1a1e044738a4d8028b0abdfd7d6ad3 to your computer and use it in GitHub Desktop.
Using Bogus.Locations to generate random points in a 3 mile radius
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Bogus" Version="28.4.4" />
<PackageReference Include="Bogus.Locations" Version="28.4.4.8" />
<PackageReference Include="FileHelpers" Version="3.4.1" />
</ItemGroup>
</Project>
using System;
using System.Linq;
using Bogus;
using Bogus.Locations;
using FileHelpers;
namespace MapMaker
{
class Program
{
static void Main(string[] args)
{
Randomizer.Seed = new Random(1337);
var faker = new Faker();
//Microsoft Campus in Redmond, WA.
double centerLat = 47.640320;
double centerLon = -122.134400;
//3 mile radius in meters:
double radiusMeters = 4828.03;
//Option 1: Using LINQ to generate 50 random points in a 3 mile radius
//--------------------------------------------------------------------
var markers = Enumerable.Range(1, 50)
.Select(_ =>
{
var code = faker.Random.Replace("????"); //some 4 alpha code
var point = faker.Location().AreaCircle(centerLat, centerLon, radiusMeters);
return new Marker
{
Code = code,
Latitude = point.Latitude,
Longitude = point.Longitude
};
});
//Option 2: Use Faker<T> to generate 50 random points in a 3 mile radius
//----------------------------------------------------------------------
var markerFaker = new Faker<Marker>()
.RuleFor(m => m.Code, f => f.Random.Replace("????"))
.Rules((f, m) =>
{
var randomPoint = f.Location().AreaCircle(centerLat, centerLon, radiusMeters);
m.Latitude = randomPoint.Latitude;
m.Longitude = randomPoint.Longitude;
});
var markers = markerFaker.Generate(50);
//Write markers to a CSV file.
// -- Import FileHelpers on NuGet
var csvEngine = new FileHelperEngine<Marker>()
{
HeaderText = $"{nameof(Marker.Code)},{nameof(Marker.Latitude)},{nameof(Marker.Longitude)}"
};
csvEngine.WriteFile(@"r:\RandomMarkers.csv", markers);
}
}
[DelimitedRecord(",")]
public class Marker
{
public string Code;
public double Latitude;
public double Longitude;
}
}
Code Latitude Longitude
FDIY 47.67576390200596 -122.14782704952722
YFKH 47.63766923980389 -122.11543163471345
TBVA 47.67542884913628 -122.15718066777634
WAMW 47.67246736597213 -122.0958649049876
TMLC 47.661256443837075 -122.17901522056565
UQYN 47.63444113798221 -122.16504795204457
NOHC 47.66021285008919 -122.08337956266723
COVF 47.632749847923414 -122.12650619751605
TYTH 47.6491265003818 -122.14817398438123
XIEF 47.63404132607069 -122.12570552194308
NWJO 47.62981363969293 -122.16377745716011
IGOD 47.62939331593543 -122.10795778012209
QTCH 47.615808584330225 -122.0993388477288
HNGA 47.65287938894067 -122.14593465067173
CMIB 47.655935544110896 -122.10296492299062
OIJY 47.66411117166924 -122.12035153048441
VOBD 47.67702909150846 -122.14611050030368
WQLK 47.6115827148164 -122.14379598559907
UPOU 47.62206542743603 -122.1871558321086
DMNL 47.658497898391055 -122.12681115366911
IHQK 47.65495578301598 -122.10247387754526
ZSEA 47.64098390562673 -122.10331829332443
IDRH 47.63853416757227 -122.14642228777268
LSUU 47.65282892995459 -122.13412045449934
PTKR 47.667386690009764 -122.10261480729679
OJCA 47.61005033972177 -122.16102672339377
ILAH 47.65810230976366 -122.13829736204838
JTDI 47.64452568441551 -122.1639211651929
AWHU 47.63611471214876 -122.19706957046384
GOZQ 47.66092115138682 -122.17751804960415
TIXB 47.65696889645172 -122.11508555830608
DHIB 47.640895710786786 -122.19606445053915
REHV 47.62211448633288 -122.14185501597171
WHHQ 47.667185780342656 -122.14854345609388
VTKJ 47.635989424936184 -122.08551779924665
PRQE 47.65985061465136 -122.11437118500334
BHGA 47.64433944956534 -122.12475356393338
LYXO 47.64209703667067 -122.14089414915003
LLCL 47.60902573948677 -122.14931334905714
ODRY 47.639596168879635 -122.15045020569204
SUOB 47.637733002758026 -122.10374102617482
DNQE 47.661813868450004 -122.09828411947211
HWCD 47.63868472863811 -122.1448497855124
BHQP 47.620048288346716 -122.0831449032188
EDYB 47.616976503284626 -122.1155924050646
LCBN 47.62918327570898 -122.12323174359028
QKMQ 47.62579651912473 -122.14925595281636
EGRZ 47.64584870981807 -122.14109243913589
LLYY 47.679212946310386 -122.14347127136853
CDWF 47.62079054783041 -122.08491905775827
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment