KML Circle Generator

6,043 views
Skip to first unread message

ink_polaroid

unread,
Apr 19, 2005, 7:17:38 PM4/19/05
to KML Discussions
At least a couple of people have shown an interest in being able to generate circle primitives. I needed to do exactly that earlier today, and because neccessity is the mother of invention...

KML Circle Generator

It's not a real solution, but it's a pretty useful hack. It'll generate a LineString path for you that looks just like a circle.

You'll need the coordinates of the center of your circle, and either a radius (in meters) or the coordinates of a point on the circumference.

I haven't tested extensively, but it seems to be fine for the most part. (Theoretically there's a chance it'll b0rk if you try to do anything near the poles, although I haven't made it do that yet.)

Comments, improvements, bug reports &c all welcome. The source is here.

[Note to math geeks: I'm using a slightly modified Haversine formula to calculate the distances. It's pretty accurate, but because it assumes a spherical earth, it grows slightly inaccurate over large distances.]

gbonny

unread,
Apr 20, 2005, 4:09:47 AM4/20/05
to KML Discussions
I'm looking for a past post that says the Keyhole globe is indeed a sphere... Still looking, maybe it's faulty memory in one of MY hemispheres.

mcshea98

unread,
Apr 23, 2005, 7:09:51 PM4/23/05
to KML Discussions
Ink_Polaroid,

Looks great to me. Here are a couple circles near the pole, but they look fine.
23967-circle.kml

ink_polaroid

unread,
Apr 24, 2005, 8:19:24 PM4/24/05
to KML Discussions
Yeah. I spent some time trying to get it to barf, and couldn't. I have ideas about how it could be improved, but I'm waiting until after the promised update before I do anything.

blt

unread,
Apr 24, 2005, 10:25:13 PM4/24/05
to KML Discussions
Inkster-
I often feel the urge to 'get lost' in keyhole. I currently do this by spinning the globe with my eyes shut, but this gets a little tedious. Why do I do this you ask? Well, if I turn off all placemarks and names, I'm forced to explore the area and eventually I find that I can make a pretty good guess where I've ended up. It's a bit of a game and a goood waste of time. The only rule is I won't allow myself to fly higher than 10-20,000 feet. Do think it would it be possible to generate a network placemark that would take you to a random place each time you click on it?
-
Brad



seer

unread,
Apr 24, 2005, 10:44:15 PM4/24/05
to KML Discussions
Very, very nice idea.

ink_polaroid

unread,
Apr 24, 2005, 11:24:17 PM4/24/05
to KML Discussions
I'll try to have something for you by this time tommorrow. (I'll just assume you want the random place to be somewhere in a hi-res area. )

ink_polaroid

unread,
Apr 25, 2005, 12:13:37 AM4/25/05
to KML Discussions
Okay. I knocked this one up in the break between Star Trek and CSI. It's not quite automatic. You'll need to right-click on the folder and hit "refresh" between views to generate another random location.

I'll refine it a little tomorrow. Right now it's a random location anywhere on the globe, so there's about a 78% chance you'll end up in an ocean.
24062-random_thingy.kml

blt

unread,
Apr 25, 2005, 1:31:42 AM4/25/05
to KML Discussions
Inky -

Awesome job and many thanks! I don't know how you can keep it from hitting ocean unless you have a big table of some kind on your server. I'm not too sure it's important since it only takes a few clicks to hit land. If you change it, you might want to set the viewpoint to around 10,000 feet, give it some tilt and a random heading.

Thanks Again,
Brad

ink_polaroid

unread,
Apr 25, 2005, 3:28:31 PM4/25/05
to KML Discussions
Try this one. I grabbed the coordinates of a few dozen hi-res areas and it should now dump you in or near one of them. Same process applies -- right click on folder and refresh for each new random location.

(The coords are *really* rough, so it ain't perfect. You should still get something interesting more often than not, though.)
24134-random_thingy_hires.kml

mcshea98

unread,
May 7, 2005, 12:01:53 PM5/7/05
to KML Discussions
Here is a variation on Ink_Polaroid's KML Circle Gen.

Source

bfsmith

unread,
May 7, 2005, 10:55:59 PM5/7/05
to KML Discussions
mcshea98,

I really like this circle generator. I appreciate the time you put into this. I see you have a link to the source, but the link was not found. Can you share the source? I would like to try some variations to this.

thanks,

Brendan

mcshea98

unread,
May 7, 2005, 11:41:08 PM5/7/05
to KML Discussions
Sorry Brendan,
I fixed it. I didn't type the whole name the first time.

bfsmith

unread,
May 8, 2005, 8:10:50 PM5/8/05
to KML Discussions
mcshea98,

Thanks! I appreciate it! I look forward to checking this out on Monday morning. And now I see you also placed a rectangle and polygon generator. Very cool.

thanks,

Brendan

mcshea98

unread,
May 9, 2005, 3:15:53 AM5/9/05
to KML Discussions
I created this interesting placemark with some of the tools from here.

Brendan,

You might want to look at my "beta versions" here. I added some capabilities, and combined all inputs into one php source file. (with some commenting)
25559-test.kml

bfsmith

unread,
May 11, 2005, 2:58:09 PM5/11/05
to KML Discussions
Thanks. I will check those out.

Brendan

bfsmith

unread,
Jun 16, 2005, 1:36:26 PM6/16/05
to KML Discussions
Hi McShea98,

I hope your move to VA has gone well. I travel to Northern VA quite often for work.

I was wondering if you will have your website back online anytime in the near future. I was going to try out some of your tools for generating polygons, but I see the website is down.

thanks,

Brendan


mcshea98

unread,
Jun 16, 2005, 7:44:56 PM6/16/05
to KML Discussions
I am currently still in Texas (with my old job), but will arrive in VA in about two weeks. If I can't get my site up then, at least I will be able to publish the source code for you to use. I am planning on upgrading some parts to use with GoogleEarth soon there after.

I should have copied the source code before taking all my stuff to VA. Hindsight is 20-20.

bfsmith

unread,
Jun 17, 2005, 9:35:04 PM6/17/05
to KML Discussions
Hi mcshea,

Thanks for the update. I hope the move goes well for you.

I look forward to using your tools when it get it back online.

thanks,

Brendan

Steve_Kotleba

unread,
Jun 21, 2005, 4:42:17 PM6/21/05
to KML Discussions
Link is dead

FuNkY_CaPrIcOrN

unread,
Jun 21, 2005, 6:36:40 PM6/21/05
to KML Discussions
Quote:

Link is dead




I guess you did not read threw the Thread.

big_hoovie

unread,
Jul 19, 2005, 10:15:52 AM7/19/05
to KML Discussions
This is pretty sweet, but I'm wondering if there is a free online service that allows you to enter gps coords, and tells you what you might be looking at(like a particular buisness, etc)

HSaber

unread,
Jul 27, 2005, 8:26:44 AM7/27/05
to KML Discussions
Make a look at this code from Keyhole, and find what you want!

Hulk

unread,
Jul 27, 2005, 8:35:31 PM7/27/05
to KML Discussions
Nice circle

Lon

unread,
Jul 29, 2005, 12:33:10 PM7/29/05
to KML Discussions
Great post polaroid. I am using it to generate approximate hazard zones around LNG (liquefied natural gas) faciities. If possible, a kml that generates a "buffer-zone" around a line/path would be extremely useful too. I don't have the know-how to do that and don't know how difficult it would be. But if it's not too much and you or someone else out there has the interest, that would be very useful and much appreciated too.

Google Earth is fantastic, and people like you are doing a great service in showing that it's a whole lot more than just flying around the world looking at cool stuff. Thanks

mcshea98

unread,
Jul 29, 2005, 12:59:26 PM7/29/05
to KML Discussions
Lon,

My website is temporarily back up. It has some polygon generators. I haven't set one up for polygons with holes yet. www.mcshea98.com/Keyhole/

Lon

unread,
Jul 29, 2005, 2:17:18 PM7/29/05
to KML Discussions
Thanks mcshea98, that will be helpful too. I appreciate your response.

glimme

unread,
Jan 19, 2006, 1:02:47 PM1/19/06
to KML Discussions
Have anyone that you know built a 3d circle generator. I can send you an example if you like. I like the idea of a single point being the source.

HookEmHorns

unread,
Jan 19, 2006, 3:44:36 PM1/19/06
to KML Discussions
Speaking of KML circle generators (since this thread came back from the dead and I don't see it mentioned in here), a tool I first started using to generate KML circles is EarthShape, which is currently freeware (I knew about this before I ever knew about ink's tool).

In the future it might cost a small amount of $$, but presumably by then, there will be added functionality to draw all sorts of shapes besides just circles.

rsgrillo

unread,
Jan 20, 2006, 11:23:42 AM1/20/06
to KML Discussions
Hi,

Please take a look at

http://paginas.terra.com.br/informatica/sgrillo/googleearth/

It draw circles, make path, put distances, routes and coordinates in descriptions, etc


I developed for my own use but it might be usefull for others

slopez

unread,
Feb 21, 2006, 6:01:37 PM2/21/06
to KML Discussions
package conversionToKML;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class KMLCircle {

void GenCircle(double centerlat_form, double centerlong_form, int num_points, double radius_form, String outputFile){
double lat1, long1, lat2, long2;
double dlat, dlong, d_rad;
double a, c, d;
double delta_pts;
double radial, lat_rad, dlon_rad, lon_rad;

double degreeToRadian = Math.PI/180.0;

// convert coordinates to radians
lat1 = Math.toRadians(centerlat_form);
long1 = Math.toRadians(centerlong_form);

//Earth measures
//Year Name a (meters) b (meters) 1/f Where Used
//1980 International 6,378,137 6,356,752 298.257 Worldwide
d = radius_form;
d_rad = d/6378137;

try{
File fileOutput = new File(outputFile);
BufferedWriter writer = new BufferedWriter(new FileWriter(fileOutput));

writer.write("<Document>\n<name>$Document</name>\n<Folder>\n<name>$Folder</name>\n<visibility>1</visibility>\n<Placemark>\n<name>$Circle_name</name>\n<visibility>$visibility</visibility>\n<Style>\n<geomColor>$geomColor1$geomColor2</geomColor>\n<geomScale>$geomScale</geomScale></Style>\n<LineString>\n<coordinates>\n");
//System.out.write(c);
//System.out.println(c);

// loop through the array and write path linestrings
for(int i=0; i<=num_points; i++) {
//delta_pts = 360/(double)num_points;
//radial = Math.toRadians((double)i*delta_pts);
radial = Math.toRadians((double)i);

//This algorithm is limited to distances such that dlon <pi/2
lat_rad = Math.asin(Math.sin(lat1)* Math.cos(d_rad) + Math.cos(lat1)* Math.sin(d_rad)* Math.cos(radial));
dlon_rad = Math.atan2(Math.sin(radial)* Math.sin(d_rad)* Math.cos(lat1), Math.cos(d_rad)- Math.sin(lat1)* Math.sin(lat_rad));
lon_rad = ((long1 + dlon_rad + Math.PI) % (2*Math.PI)) - Math.PI;

//write results
writer.write( Math.toDegrees(lon_rad) + ", ");
writer.write( Math.toDegrees(lat_rad) + ", 0");
writer.write('\n');
}
// output footer
writer.write("</coordinates>\n</LineString>\n</Placemark>\n</Folder>\n</Document>");

writer.close();
}catch (IOException e){
return;
}
}
/**
* @param args
*/
public static void main(String args) {
KMLCircle myCircle = new KMLCircle();
myCircle.GenCircle(37.422000, -122.084073, 360, 10000, "CircleTest.kml");
}

}

Eliot_Christian

unread,
Apr 17, 2006, 6:26:37 AM4/17/06
to KML Discussions
A KML Polygon might be more approriate than a KML LineString when approximating a circle, as both a circle and a polygon are closed figures.

A Polygon is important when you need the circle to have holes (e.g., the "buffer zone" mentioned in another post on this thread). A hole is defined using the Polygon child element <innerBoundaryIs>. LineString does not have such a child element, of course.


.

Eliot_Christian

unread,
Apr 17, 2006, 2:25:31 PM4/17/06
to KML Discussions
The precision of the coordinate values used for the vertices should be determined with respect to the coordinates precision of the circle center and the length of the circle radius. This precision can then be applied through a Round function (e.g., see attached PHP code.)

For my application, I use 36-sided polygons to approximate circles. Each vertex for a given polygon side therefore needs to be at least two decimal places more precise than the center latitude or the center longitude (whichever is most precise).

But, a shorter radius can impose the need for more precision than is implied by the center coordinates. I handle the precision determined by radius length using a simple range test:

if ($radius > 10000000) { $radiusPrecision = 0;
} elseif ($radius > 1000000) { $radiusPrecision = 1;
} elseif ($radius > 100000) { $radiusPrecision = 2;
} elseif ($radius > 10000) { $radiusPrecision = 3;
} elseif ($radius > 1000) { $radiusPrecision = 4;
} elseif ($radius > 100) { $radiusPrecision = 5;
} elseif ($radius > 10) { $radiusPrecision = 6;
} else { $radiusPrecision = 7;
}

Here's my rationale for handling the radius this way: A circle with radius greater than 10,000 kilometers covers a hemsiphere and needs only whole number coordinates (zero decimal places) to precisely divide a polygon with dozens to hundreds of sides. As the circle radius decreases, one needs correspondingly higher precision. For radii less than ten meters, a precision of seven decimal places is sufficient. (Google Earth doesn't seem to deal with objects at scales under a meter anyway, AFAIK).
389636-circlegen.zip

stebi61

unread,
Sep 19, 2006, 3:18:24 AM9/19/06
to KML Discussions
Hello

Great job you did, it works nearly perfect for my. The failure the formula calculate is about 1000 m each 1000 km, so pretty accurate.

I am in the process to build three circle around certain airfields. I.E. St. Johns Airport. 1 circle represent 60 min. with a given speed, 1 circle represent 120 ,in. with a given speed and 1 circle represent 180 min. with a given speed. So at the end it will have hundreds of circle around the earth.

Some missing improvements:
Possibility to colorize the full circle (not only the boundary)
Possibility to import places in to the calculator to get Lat and Long in one step.
Possibility to store content. i.E.. Different distances
Possibility to calculate circles for more than one place and more than one distance.

Again it works pretty good and I am happy to found a solution

many Thanks

Thomas

ink_polaroid

unread,
Sep 19, 2006, 3:51:14 AM9/19/06
to KML Discussions
Whoo... here's a blast from the past.

(Calling all KML Tools developers ... your audience awaits...)

barryhunter

unread,
Sep 19, 2006, 5:39:06 AM9/19/06
to KML Discussions
Still not perfect, or fitting exactly what you want to do, but I KNOW* many people use, is second point in 8 on:

http://www.nearby.org.uk/google.html#8

This will maintain a circle in the center of the view. As its a network link you can open multiple copies, and each one has a configurable colour, and also radius (right-click and choose properties, and edit the URL)

So in your case setup a number of links as required, then move around the earth, and when the circles are as you want them, just Copy the placemark(s), and paste it into a folder.

As I say still not perfect, but possibly easier than copy/pasting all the lat/longs and setting the variables as you need. If interest could add a filled polygon option


* (in fact its almost the most used tool on the website, but that might be by accident, anyway I digress!)

stebi61

unread,
Sep 19, 2006, 3:30:28 PM9/19/06
to KML Discussions
Woow... I am speekless.

This is so great, so fast and (I did a short test) for 1100 sm it calculate 1101.33 sm, again perfect match.

For an greenhorn like my, it would be easier when somewhere the color-code is explained and if there is a color filling possibility available. Rest is realy selfexplaining.

Rest is great and I am wondering about the people here, fabulous

svens

unread,
Sep 19, 2006, 4:57:56 PM9/19/06
to KML Discussions
Check this thread too.
Reply all
Reply to author
Forward
0 new messages