我目前正在尝试使用 ruby (Nokogiri) 解析一个非常大的 kml (xml) 文件,但遇到了一些麻烦。
解析代码很好,事实上我只是为了它而分享它,即使这段代码与我的问题没有太大关系:
geofactory = RGeo::Geographic.projected_factory(:projection_proj4 => "+proj=lcc +lat_1=34.83333333333334 +lat_2=32.5 +lat_0=31.83333333333333 +lon_0=-81 +x_0=609600 +y_0=0 +ellps=GRS80 +to_meter=0.3048 +no_defs", :projection_srid => 3361)
f = File.open("horry_parcels.kml")
kmldoc = Nokogiri::XML(f)
kmldoc.css("//Placemark").each_with_index do |placemark, i|
puts i
tds = Nokogiri::HTML(placemark.search("//description").children[0].to_html).search("tr > td")
h = HorryParcel.new
h.owner_name = tds.shift.text
tds.shift
tds.each_slice(2) do |k, v|
col = k.text.downcase
eval("h.#{col} = v.text")
end
coords = kmldoc.search("//MultiGeometry")[i].text.gsub("\n", "").gsub("\t", "").split(",0 ").map {|x| x.split(",")}
points = coords.map { |lon, lat| geofactory.parse_wkt("POINT (#{lon} #{lat})") }
geo_shape = geofactory.polygon(geofactory.linear_ring(points))
proj_shape = geo_shape.projection
h.geo_shape = geo_shape
h.proj_shape = proj_shape
h.save
end
无论如何,我已经用一个小得多的 kml 样本测试了这段代码,它可以工作。
然而,当我加载真实的东西时,ruby 只是等待,好像它正在处理一些东西。然而,这个“处理”现在已经跨越了几个小时,而我一直在做其他事情。您可能已经注意到,我在 Placemarks 数组上有一个计数器 ( ),在这几个小时的时间里,命令行中each_with_index
没有一个i
值。put
奇怪的是它还没有超时,但即使这有效,也必须有更好的方法来做这件事。
我知道我可以在 Google 地球(此处为 Google 地球专业版)中打开 KML 文件并将数据保存在更小、更易于管理的 kml 文件中,但从看起来设置的方式来看,这将是一个非常手动的、不专业的过程。
如果有帮助,这里是一个 kml 示例(只有一个地标)。
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<name>justone.kml</name>
<Style id="PolyStyle00">
<LabelStyle>
<color>00000000</color>
<scale>0</scale>
</LabelStyle>
<LineStyle>
<color>ff0000ff</color>
</LineStyle>
<PolyStyle>
<color>00f0f0f0</color>
</PolyStyle>
</Style>
<Folder>
<name>justone</name>
<open>1</open>
<Placemark id="ID_010161">
<name>STUART CHARLES A JR</name>
<Snippet maxLines="0"></Snippet>
<description>""</description>
<styleUrl>#PolyStyle00</styleUrl>
<MultiGeometry>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-78.941896,33.867893,0 -78.942514,33.868632,0 -78.94342899999999,33.869705,0 -78.943708,33.870083,0 -78.94466799999999,33.871142,0 -78.94511900000001,33.871639,0 -78.94541099999999,33.871776,0 -78.94635,33.872216,0 -78.94637899999999,33.872229,0 -78.94691400000001,33.87248,0 -78.94708300000001,33.87256,0 -78.94783700000001,33.872918,0 -78.947889,33.872942,0 -78.948655,33.873309,0 -78.949589,33.873756,0 -78.950164,33.87403,0 -78.9507,33.873432,0 -78.95077000000001,33.873384,0 -78.950867,33.873354,0 -78.95093199999999,33.873334,0 -78.952518,33.871631,0 -78.95400600000001,33.869583,0 -78.955254,33.867865,0 -78.954606,33.867499,0 -78.953833,33.867172,0 -78.952994,33.866809,0 -78.95272799999999,33.867129,0 -78.952139,33.866803,0 -78.95152299999999,33.86645,0 -78.95134299999999,33.866649,0 -78.95116400000001,33.866847,0 -78.949281,33.867363,0 -78.948936,33.866599,0 -78.94721699999999,33.866927,0 -78.941896,33.867893,0
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</MultiGeometry>
</Placemark>
</Folder>
</Document>
</kml>
编辑:我使用的 99.9% 的数据都是*.shp
格式的,所以在过去的一周里我只是忽略了这个问题。但是我要让这个过程在我的台式计算机上运行(离开我的笔记本电脑)并运行它直到它超时或完成。
class ClassName
attr_reader :before, :after
def go
@before = Time.now
run_actual_code
@after = Time.now
puts "process took #{(@after - @before) seconds} to complete"
end
def run_actual_code
...
end
end
上面的代码应该告诉我花了多长时间。从那(如果它确实完成)我们应该能够计算出一个粗略的经验法则,即在没有 SAX 解析或文档文本组件的“原子化”的情况下,您应该期望您的(否则为 PERFECT)代码运行多长时间。