Hee guys,
I'm currently working with a kml file which I use a xpath parser on. However some files are different then other.
The first file has 2 different styles: Style1 and Style2. However in most files like this these style1 and style2 are different linked. What I mean with this is that the style1 in the linked file underneath here is in some other files style2. So if I want to hard code it to get style1, it still won't work on all files.
The second file has just one style and this one works correct.
So the point of these files is to get the style with the color and the coordinates with multiple values (Style2). However I'm very new to xPath and I have no idea how I can check which style has a color attribute and get the style value and then get the multiple coordinates with this style attribute. (If you don't understand this please comment and I'll try to explain it on a different way).
File one : (this doesn't work yet).
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://ift.tt/1pOyDmC">
<Document>
<name>Wandelroute Molenwijk</name>
<description><![CDATA[]]></description>
<Style id="style1">
<IconStyle>
<Icon>
<href>http://ift.tt/1DZD9FU;
</Icon>
</IconStyle>
</Style>
<Style id="style2">
<LineStyle>
<color>ff1b1a1f</color>
<width>10</width>
</LineStyle>
</Style>
<Placemark>
<name>Startpunt wandelroute</name>
<description><![CDATA[<div dir="ltr">Startpunt van de wandelroute bij de Montessorischool</div>]]></description>
<styleUrl>#style1</styleUrl>
<Point>
<coordinates>4.824704,52.663391,0.000000</coordinates>
</Point>
</Placemark>
<Placemark>
<name>Wandelroute Molenwijk</name>
<description><![CDATA[]]></description>
<styleUrl>#style2</styleUrl>
<LineString>
<tessellate>1</tessellate>
<coordinates>
4.824736,52.663383,0.000000
4.825798,52.662617,0.000000
4.825938,52.662231,0.000000
4.826034,52.662102,0.000000
4.826335,52.661938,0.000000
4.826528,52.661732,0.000000
4.826474,52.661568,0.000000
4.826292,52.661381,0.000000
4.826163,52.661308,0.000000
4.826463,52.661064,0.000000
4.826356,52.660801,0.000000
4.826227,52.660538,0.000000
4.826066,52.660202,0.000000
4.827000,52.660168,0.000000
4.827332,52.659935,0.000000
4.826775,52.659641,0.000000
4.827665,52.658951,0.000000
4.827676,52.658730,0.000000
4.828255,52.658691,0.000000
4.828770,52.658581,0.000000
4.829135,52.658192,0.000000
4.828963,52.657879,0.000000
4.828920,52.657436,0.000000
4.828985,52.657200,0.000000
4.828899,52.656891,0.000000
4.828942,52.656654,0.000000
4.829221,52.656395,0.000000
4.829564,52.656200,0.000000
4.830723,52.656277,0.000000
4.831195,52.656654,0.000000
4.831753,52.656822,0.000000
4.832225,52.657097,0.000000
4.832139,52.657513,0.000000
4.831839,52.657761,0.000000
4.831302,52.657825,0.000000
4.831109,52.658073,0.000000
4.831173,52.658436,0.000000
4.831173,52.658932,0.000000
4.830101,52.659023,0.000000
4.829950,52.658932,0.000000
4.829929,52.658661,0.000000
4.829757,52.658646,0.000000
4.829500,52.658684,0.000000
4.829371,52.658684,0.000000
4.829221,52.658592,0.000000
4.828920,52.658592,0.000000
4.828770,52.658581,0.000000
</coordinates>
</LineString>
</Placemark>
<Folder><Placemark><Style><LineStyle><color>ff1b1a1f</color></LineStyle></Style></Placemark></Folder></Document>
</kml>
File 2: (this one is working)
<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://ift.tt/Wr35qg">
<Document><Folder><name>Route</name>
<Schema name="Route" id="Route">
<SimpleField name="Name" type="string"/>
<SimpleField name="Description" type="string"/>
<SimpleField name="name" type="string"/>
<SimpleField name="cmt" type="string"/>
<SimpleField name="desc" type="string"/>
<SimpleField name="src" type="string"/>
<SimpleField name="link1_href" type="string"/>
<SimpleField name="link1_text" type="string"/>
<SimpleField name="link1_type" type="string"/>
<SimpleField name="link2_href" type="string"/>
<SimpleField name="link2_text" type="string"/>
<SimpleField name="link2_type" type="string"/>
<SimpleField name="number" type="int"/>
<SimpleField name="type" type="string"/>
<SimpleField name="gpxx_Track" type="string"/>
</Schema>
<Placemark>
<name>Route</name>
<Style><LineStyle><color>ff00ccff</color></LineStyle><PolyStyle><fill>0</fill></PolyStyle></Style>
<ExtendedData><SchemaData schemaUrl="#Route">
<SimpleData name="Name">Route</SimpleData>
<SimpleData name="gpxx_Track"><gpxx:DisplayColor>Red</gpxx:DisplayColor></SimpleData>
</SchemaData></ExtendedData>
<LineString><coordinates>6.1879098,52.2997009,0 6.187073,52.3000289,0 6.1867726,52.300042,0 6.1861503,52.2995499,0 6.1858821,52.2991628,0 6.185056,52.2992744,0 6.1833823,52.2994187,0 6.1836183,52.2990907,0 6.1845732,52.2980015,0 6.1845732,52.2975357,0 6.1845624,52.2974832,0 6.1850238,52.2964596,0 6.1850131,52.2964268,0 6.1841118,52.2965515,0 6.1831033,52.2963284,0 6.1825776,52.2980146,0 6.182524,52.2987495,0 6.1825776,52.2994581,0 6.1822665,52.2994909,0 6.1826098,52.3009212,0 6.182642,52.301492,0 6.1831999,52.3023252,0 6.1836827,52.3022399,0 6.1841655,52.3022005,0 6.1847878,52.3022333,0 6.1853564,52.3022005,0 6.1859787,52.3023121,0 6.1867404,52.3021349,0 6.1870623,52.3015838,0 6.1878133,52.3013542,0 6.18806,52.3013608,0 6.1885321,52.3011902,0 6.1904097,52.3008228,0 6.1907101,52.3010786,0 6.1908603,52.3011246,0 6.1913431,52.3013411,0 6.1920834,52.3014854,0 6.1937141,52.3017347,0 6.1938858,52.2991235,0 6.1928558,52.2993072,0 6.1925125,52.2992941,0 6.1918688,52.2993859,0 6.1913109,52.2996156,0 6.1906779,52.2999174,0 6.1906779,52.2998386,0 6.1905277,52.2995303,0 6.1902165,52.2993072,0 6.189959,52.2992744,0 6.1896479,52.2990251,0 6.1895514,52.2988085,0 6.1890256,52.2986773,0 6.1880171,52.2994581,0 6.1879098,52.299668,0 </coordinates></LineString>
</Placemark>
</Folder></Document></kml>
Code which gets the attributes to display this Route. This only works on the second file.
public class KMLTask extends AsyncTask<String, Void, String> {
private int mRouteId;
private String color;
private OnRouteLatLngResult listener;
public KMLTask(int id) {
mRouteId = id;
}
public KMLTask(OnRouteLatLngResult listener) {
this.listener = listener;
}
@Override
protected String doInBackground(String... params) {
String coordinates = null;
try {
URL u = new URL(ApiController.BASE_FILE_URL + params[0]);
XPath xPath = XPathFactory.newInstance().newXPath();
coordinates = xPath.evaluate("//*[local-name()='coordinates']",
new InputSource(u.openStream()));
color = xPath.evaluate("//*[local-name()='color']",
new InputSource(u.openStream()));
} catch (MalformedURLException e) {
Log.e(TAG, "Malformed URL!", e);
} catch (XPathExpressionException e) {
Log.e(TAG, "XPathExpression Exception!", e);
} catch (IOException e) {
Log.e(TAG, "IOException!", e);
}
return coordinates;
}
private String[] splitStringEvery(String s, int interval) {
int arrayLength = (int) Math
.ceil(((s.length() / (double) interval)));
String[] result = new String[arrayLength];
int j = 0;
int lastIndex = result.length - 1;
for (int i = 0; i < lastIndex; i++) {
result[i] = s.substring(j, j + interval);
j += interval;
} // Add the last bit
result[lastIndex] = s.substring(j);
return result;
}
@Override
protected void onPostExecute(String coordinates) {
Log.d("Cursor", color);
if (coordinates != null) {
Matcher m = Pattern.compile("([0-9.]+),([0-9.]+)").matcher(
coordinates);
// LatLngBounds.Builder builder = new LatLngBounds.Builder();
PolylineOptions rectOptions = new PolylineOptions();
if (color.length() > 0) {
String[] arColor = splitStringEvery(color, 2);
int lineColor = Color.parseColor("#" + arColor[3]
+ arColor[2] + arColor[1]);
rectOptions.color(lineColor);
} else {
rectOptions.color(Color.RED);
}
while (m.find()) {
LatLng ll = new LatLng(Double.parseDouble(m.group(2)),
Double.parseDouble(m.group(1)));
rectOptions.add(ll);
// builder.include(ll);
}
m.reset();
m.find();
LatLng latlong = new LatLng(Double.parseDouble(m.group(2)),
Double.parseDouble(m.group(1)));
// mItem.mRoute.mTmpLatLng = Double.parseDouble(m.group(2)) +
// "," +
// Double.parseDouble(m.group(1));
if (listener != null) {
listener.onResult(latlong);
return;
}
addMarkerRoute(mRouteId, latlong);
addPolyline(mRouteId, rectOptions);
// CameraUpdate cu =
// CameraUpdateFactory.newLatLngBounds(builder.build(),
// LOCATION_PADDING);
// mGoogleMap.moveCamera(cu);
setMapBounds();
}
}
}
No comments:
Post a Comment